home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / doc / dice_libraries.doc < prev    next >
Text File  |  1994-02-13  |  202KB  |  7,659 lines

  1.  
  2. dice/AslBase,DiceCacheBase,DiskfontBase,DOSBase,FifoBase,GadToolsBase,IconBase,IntuitionBase,LayersBase,MathBase,MathIeeeDoubBasBase,MathIeeeDoubTransBase,MathIeeeSingBasBase,MathIeeeSingTransBase,MathTransBase,RexxSysBase,SysBase,TimerBase,TranslatorB
  3. se,UtilityBase
  4.  
  5.     FUNCTION
  6.     DICE will automatically open libraries for you (DICE)
  7.  
  8.     DESCRIPTION
  9.     With DICE you need not worry about opening or closing Amiga
  10.     libraries.  You don't have to deal with the mess and hassle of
  11.     checking error returns from each open.    If a function call to a
  12.     library is used, the library base variable will be referenced.    If
  13.     you did not explicitly define the variable, DICE will insert it,
  14.     along with code to open and close the library.
  15.  
  16.     For example, if the _IntuitionBase base variable is referenced (say,
  17.     with extern) but not declared then _IntuitionBase will be
  18.     automatically declared in auto.lib.  Additionally, auto.lib adds
  19.     routines to the autoinit and autoexit sequences that automatically
  20.     open "intuition.library" before _main and close it after _exit. If
  21.     the auto-open fails the program will be aborted before _main is ever
  22.     called. The autoexit routine that closes the library first checks to
  23.     see if the base variable is NULL and skips trying to close the
  24.     library if so.
  25.  
  26.     EXAMPLE
  27.     /*
  28.     **  Example program which just calls an
  29.     **  intuition.library function without
  30.     **  bothering to open the library.
  31.     */
  32.     main()
  33.     {
  34.        DisplayBeep( 0 );
  35.     }
  36.  
  37. dice/abort                                                        dice/abort
  38.  
  39.     FUNCTION
  40.     abort a program with an error (ANSI)
  41.  
  42.     SYNTAX
  43.     #include <stdlib.h>
  44.     void abort(void);
  45.  
  46.     DESCRIPTION
  47.     abort aborts a program with a non-zero exit code.  The default abort
  48.     routine in c.lib does the equivalent of an exit(20);. Programmers may
  49.     overide the default abort routine with thier own.
  50.  
  51.     RESULTS
  52.     abort never returns
  53.  
  54.     SEE ALSO
  55.     assert
  56.  
  57.     EXAMPLE
  58.     #include <stdio.h>
  59.     #include <stdlib.h>
  60.     main(int ac, char**av)
  61.     {
  62.        if (ac == 1) {
  63.           puts("Hey, I expected a parameter!");
  64.           abort();
  65.           }
  66.           puts("Thanks!");
  67.           return(0);
  68.     }
  69.  
  70. dice/abs,labs                                                  dice/abs,labs
  71.  
  72.     FUNCTION
  73.     take absolute value (ANSI)
  74.  
  75.     SYNTAX
  76.     #include <stdio.h>
  77.     #include <stdlib.h>
  78.     int r = [l]abs(n);
  79.     int n;
  80.  
  81.     DESCRIPTION
  82.     abs takes the absolute value of an int; labs takes the absolute value
  83.     of a long.  Both return the absolute value of the specified integer.
  84.     For example, r = n if n >= 0, r = -n (positive) if n < 0. Normally
  85.     one would not use this call due to overhead, but it exists for
  86.     compatibility.
  87.  
  88.     ## WARNING: The absolute value of 0x80000000 cannot be taken.
  89.     ## 0x80000000 will be returned.
  90.  
  91.     || NOTE: UNDER DICE, sizeof(int) == sizeof(long) and these two
  92.     || functions are thus identical.
  93.  
  94.     INPUTS
  95.     int n;            integer
  96.  
  97.     RESULTS
  98.     int r;            absolute value of integer
  99.  
  100.     EXAMPLE
  101.     #include<stdio.h>
  102.     #include <stdlib.h>
  103.     main()
  104.     {
  105.        int n = -53;
  106.        printf("The absolute value of %d is %d\n", n, abs(n));
  107.        sleep(1);
  108.        printf("But its faster if you write a macro:\n");
  109.        #define abs(x)  (((x) < 0) ? -(x) : (x))
  110.        sleep(1);
  111.        printf("The absolute value of %d is %d\n", n, abs(n));
  112.        return(0);
  113.     }
  114.  
  115. dice/access                                                      dice/access
  116.  
  117.     FUNCTION
  118.     determine whether file is accessable (UNIX)
  119.  
  120.     SYNTAX
  121.     #include <stdio.h>
  122.     int r = access(filename, mode);
  123.     const char *filename;
  124.     int mode;
  125.  
  126.     DESCRIPTION
  127.     access returns 0 upon success, -1 if access with the requested modes
  128.     was impossible.  The filename serves as a pointer to the string of
  129.     the filename we wish to check, and the modes are what we expect the
  130.     file to be able to do.    The modes may be one or more of the following
  131.     OR'd together.
  132.  
  133.      0   check for existance of file only
  134.      1   check execute permission for file
  135.      2   check write permission for file
  136.     4   check read permission for file
  137.  
  138.     INPUTS
  139.     char *filename;     file to check
  140.  
  141.     int mode;        modes as specified above
  142.  
  143.     RESULTS
  144.     int r;            0 if modes available, -1 if not
  145.  
  146.     SEE ALSO
  147.     open, fopen
  148.  
  149.     EXAMPLE
  150.     #include <stdio.h>
  151.  
  152.     main(int ac, char **av)
  153.     {
  154.        char *name;
  155.        if (ac == 1)
  156.        {
  157.           puts("Expected a file name argument");
  158.           exit(1);
  159.        }
  160.        name = av[1];
  161.        if (access(name, 0) == 0) {
  162.          puts("It exists");
  163.           if (access(name, 1) == 0)
  164.          puts("It is executable");
  165.           if (access(name, 2) == 0)
  166.          puts("I can write to it!");
  167.           if (access(name, 4) == 0)
  168.          puts("I can even read from it!");
  169.        }
  170.        else
  171.          puts("Hmmm, that file does not exist");
  172.     }
  173.  
  174. dice/acos                                                          dice/acos
  175.  
  176.     FUNCTION
  177.     take arc cosine (ANSI)
  178.  
  179.     LIBRARY
  180.     m.lib
  181.  
  182.     SYNTAX
  183.     #include <math.h>
  184.     double a = acos(b);
  185.     double b;
  186.  
  187.     DESCRIPTION
  188.     acos returns the arc cosine of a double quantity.
  189.  
  190.     INPUTS
  191.     double b;        double floating point value
  192.  
  193.     RESULTS
  194.     double a;        result double floating point value
  195.  
  196.     SEE ALSO
  197.     asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan, facos,
  198.     fasin
  199.  
  200.     EXAMPLE
  201.     /*
  202.      *  compile with the math library -lm
  203.      */
  204.     #include <math.h>
  205.     #include <stdio.h>
  206.     main()
  207.     {
  208.        {
  209.           double a = acos(0.25);
  210.           printf("acos 0.25 = %lf\n", a); /* 1.318   */
  211.        }
  212.        {  /* less accuracy     */
  213.           float a = facos(0.25);
  214.           printf("acos 0.25 = %lf\n", (double)a);
  215.        }
  216.        return(0);
  217.     }
  218.  
  219. dice/alloca                                                      dice/alloca
  220.  
  221.     FUNCTION
  222.     allocate memory from the stack (UNIX)
  223.  
  224.     SYNTAX
  225.     #include <stdlib.h>
  226.     void *ptr = alloca(long bytes);
  227.  
  228.     DESCRIPTION
  229.     alloca comes from the UNIX world.  It allocates memory off the stack
  230.     for use within a procedure.  The allocated memory is automatically
  231.     freed when the subroutine returns.
  232.  
  233.     :: Beginner's Note: Do not use alloca if you can help it; alloca is
  234.     :: not easily portable across machines.
  235.  
  236.     || NOTE: When a low stack condition arises, alloca will abort by
  237.     || printing an error message and calling abort;  alloca does not
  238.     || currently try to allocate dynamic memory when it runs out of
  239.     || stack. Some implementations of alloca use alloca(0) to free
  240.     || allocated stack. This feature is not currently implemented in
  241.     || DICE's alloca call.
  242.  
  243.     SEE ALSO
  244.     setjmp, longjmp
  245.  
  246.     EXAMPLE
  247.     #include <alloca.h>
  248.     #include <stdio.h>
  249.     main(int ac, char*av[])
  250.     {
  251.        char *ptr;
  252.        if (ac == 1) {
  253.           puts("I need test string");
  254.           exit(1);
  255.        }
  256.        ptr = alloca(strlen(av[1]) + 8);
  257.        sprintf(ptr, "FOO.%s", av[1]);
  258.        puts(ptr);
  259.        return(0);
  260.     }
  261.  
  262. dice/asctime                                                    dice/asctime
  263.  
  264.     FUNCTION
  265.     convert broken down time into standard text (ANSI)
  266.  
  267.     SYNTAX
  268.     #include <time.h>
  269.     char *str = asctime(ts);
  270.     const struct tm *ts;
  271.  
  272.     DESCRIPTION
  273.     asctime converts a broken down time in the tm structure to an ASCII
  274.     string and returns a pointer to that string. The time string format
  275.     is:
  276.  
  277.     on Dec 8 01:53:33 1987\n\0
  278.  
  279.     where \n stands for a newline character and \0 is a terminating NULL.
  280.     The string is stored in a static buffer shared by both asctime and
  281.     ctime and so will be overwritten whenever either function is called.
  282.  
  283.     INPUTS
  284.     struct tm *ts;        pointer to a broken down time structure
  285.  
  286.     RESULTS
  287.     char *str;        pointer to static string
  288.  
  289.     SEE ALSO
  290.     time, localtime, asctime, strftime, ctime, clock
  291.  
  292.     EXAMPLE
  293.     #include <stdio.h>
  294.     #include <time.h>
  295.  
  296.     main()
  297.     {
  298.        time_t t = time(NULL);
  299.        fputs(asctime(localtime(&t)), stdout);
  300.        return(0);
  301.     }
  302.  
  303. dice/assert                                                      dice/assert
  304.  
  305.     FUNCTION
  306.     assert that an expression is true,  else abort (ANSI)
  307.  
  308.     SYNTAX
  309.     #include <assert.h>
  310.     assert(condition);   /* MACRO */
  311.  
  312.     DESCRIPTION
  313.     assert checks the condition and if not true prints an error message
  314.     indicating the source filename and line number that the assertion
  315.     failed at, and then aborts.  The DICE version of assert generates a
  316.     single static string in assert.h for each module containing the file
  317.     name. Multiple usages of assert refer to the same physical filename
  318.     string.
  319.  
  320.     INPUTS
  321.     expression;        an expression which the macro negates.
  322.  
  323.     SEE ALSO
  324.     abort
  325.  
  326.     EXAMPLE
  327.     #include <assert.h>
  328.  
  329.     main(int ac, char **av)
  330.     {
  331.        assert(ac > 1); /*  expect at least one argument! */
  332.        return(0);
  333.     }
  334.  
  335. dice/asin                                                          dice/asin
  336.  
  337.     FUNCTION
  338.     take the arc sine of a double quantity (ANSI)
  339.  
  340.     LIBRARY
  341.     m.lib
  342.  
  343.     SYNTAX
  344.     #include <math.h>
  345.     double a = asin(b);
  346.     double b;
  347.  
  348.     DESCRIPTION
  349.     asin takes the arc sine of a floating point quantity.
  350.  
  351.     INPUTS
  352.     double b;        double floating point value
  353.  
  354.     RESULTS
  355.     double a;        result double floating point value
  356.  
  357.     SEE ALSO
  358.     acos, cos, exp, fabs, log, log10, pow, sin, sqrt, tan facos, fasin
  359.  
  360.     EXAMPLE
  361.     /*
  362.     *  compile with the math library -lm
  363.     */
  364.     #include <math.h>
  365.     #include <stdio.h>
  366.     main()
  367.     {
  368.        {
  369.           double a = asin(0.25);
  370.           printf("asin 0.25 = %lf\n", a);  /* 0.2527 */
  371.        }
  372.        {  /* less accuracy    */
  373.           float a = fasin(0.25);
  374.           printf("asin 0.25 = %lf\n", (double)a);
  375.        }
  376.        return(0);
  377.     }
  378.  
  379. dice/atan                                                          dice/atan
  380.  
  381.     FUNCTION
  382.     take arc tan of a double quantity (ANSI)
  383.  
  384.     LIBRARY
  385.     m.lib
  386.  
  387.     SYNTAX
  388.     #include <math.h>
  389.     double a = atan(b);
  390.     double b;
  391.  
  392.     DESCRIPTION
  393.     atan takes the arc tangent of a floating point quantity.
  394.  
  395.     INPUTS
  396.     double b;        double floating point value
  397.  
  398.     RESULTS
  399.     double a;        result double floating point value
  400.  
  401.     SEE ALSO
  402.     acos, asin, cos, exp, fabs, log, log10, pow, sin, sqrt, tan facos,
  403.     fasin
  404.  
  405.     EXAMPLE
  406.     /*
  407.      *  compile with the math library -lm
  408.      */
  409.     #include <math.h>
  410.     #include <stdio.h>
  411.  
  412.     main()
  413.     {
  414.        {
  415.           double a = atan(0.25);
  416.           printf("atan 0.25 = %lf\n", a);  /* 0.245   */
  417.        }
  418.        {  /*  less accuracy   */
  419.           float a = fatan(0.25);
  420.           printf("atan 0.25 = %lf\n", (double)a);
  421.        }
  422.        return(0);
  423.     }
  424.  
  425. dice/atexit                                                      dice/atexit
  426.  
  427.     FUNCTION
  428.     specify routine that is automatically called on exit (ANSI)
  429.  
  430.     SYNTAX
  431.     #include <stdio.h>
  432.     #include <stdlib.h>
  433.     int error = atexit(funcptr);
  434.     void (*fptr)(void);
  435.  
  436.     DESCRIPTION
  437.     The atexit routine adds a function to the list of functions called
  438.     when the program exits.  The atexit routine is called before stdio
  439.     and file descriptors are closed down.  This exit function is called
  440.     whenever the program exits, even if main returns normally. atexit
  441.     will return 0 on success, -1 on failure. Some systems limit the
  442.     number of atexit functions one can add (DICE does not) so if you add
  443.     more than one you should check the return value.
  444.  
  445.     INPUTS
  446.     void (*fptr)(void);
  447.                 routine to add to exit call list, takes no
  448.                 arguments and returns nothing.
  449.  
  450.     RESULTS
  451.     int error;        0 on success, -1 on failure.
  452.  
  453.     SEE ALSO
  454.     onbreak
  455.  
  456.     EXAMPLE
  457.     /*
  458.      *  Atexit is useful to free up resources that would
  459.      *  otherwise not be freed up by DICE. For example, any
  460.      *  thing AllocMem'd.  The atexit function is called on
  461.      *  any exit ... return from main, call to exit, or ^C.
  462.      *
  463.      *  Normally your atexit routine cannot make assumptions
  464.      *  as to what has been allocated and what has not since
  465.      *  exit can be called from anywhere in the program.
  466.      */
  467.     #include <stdio.h>
  468.     #include <stdlib.h>
  469.     extern void *AllocMem();
  470.     void *MemPtr; long MemLen;
  471.  
  472.     void myexit(void)
  473.     {
  474.        if (MemPtr) /*  only if it is allocated */
  475.            FreeMem(MemPtr, MemLen);
  476.        MemPtr = NULL;
  477.     }
  478.  
  479.     /*  Now we can take a ^C anywhere... before we allocate,
  480.      *  after, or even after we free (note I am careful to set
  481.      *  MemPtr back to NULL!)
  482.      */
  483.     main()
  484.     {
  485.        short i;
  486.        atexit(myexit);
  487.  
  488.        for (i = 0; i < 50; ++i)
  489.           printf("Before %d\t(%ld)\n", i, AvailMem(0) );
  490.        MemLen = 32;
  491.        MemPtr = AllocMem(MemLen, 0);
  492.        if (! MemPtr) {
  493.           exit(20);
  494.        }
  495.  
  496.        for (i = 0; i < 50; ++i)
  497.           printf("During %d\t(%ld)\n", i, AvailMem(0) );
  498.  
  499.        FreeMem(MemPtr, MemLen);
  500.        MemPtr = NULL; /* Mark as freed */
  501.  
  502.        for (i = 0; i < 50; ++i)
  503.           printf("After  %d\t(%ld)\n", i, AvailMem(0) );
  504.        return(0);
  505.     }
  506.  
  507. dice/atof                                                          dice/atof
  508.  
  509.     FUNCTION
  510.     convert string into double floating point value (ANSI)
  511.  
  512.     SYNTAX
  513.     #include <stdio.h>
  514.     #include <stdlib.h>
  515.     double d = atof(str);
  516.     const char *str;
  517.  
  518.     DESCRIPTION
  519.     atof converts a string into a double floating point value; it is
  520.     equivalent to calling strtod(str, NULL).  Please refer to strtod for
  521.     more information.
  522.  
  523.     INPUTS
  524.     char *str;        string, like "1.234E-4";
  525.  
  526.     RESULTS
  527.     double d;        double fp representation of string
  528.  
  529.     SEE ALSO
  530.     strtod
  531.  
  532. dice/atoi,atol                                                dice/atoi,atol
  533.  
  534.     FUNCTION
  535.     convert string into integer (ANSI)
  536.  
  537.     SYNTAX
  538.     #include <stdio.h>
  539.     #include <stdlib.h>
  540.     int x =atoi(str);
  541.     long y = atol(str);
  542.     const char *str;
  543.  
  544.     DESCRIPTION
  545.     atoi and atol convert a string of decimal integers into an integer.
  546.     It skips initial white space, processes an optional negative sign
  547.     ('-'), processes digits '0' - '9', and returns the integer. atoi and
  548.     atol have been superseded by the strtol function which can handle
  549.     numbers of any base.
  550.  
  551.     || NOTE: Under DICE, sizeof(int) == sizeof(long), and thus atoi and
  552.     || atol are exactly the same.
  553.  
  554.     INPUTS
  555.     char *str;        string to convert to int
  556.  
  557.     RESULTS
  558.     int x;            integer result long y; integer result
  559.  
  560.     SEE ALSO
  561.     strtol
  562.  
  563.     EXAMPLE
  564.     #include <stdio.h>
  565.     #include <stdlib.h>
  566.     main()
  567.     {
  568.        int i = atoi("  \t\t -123");
  569.        printf("i = %d (-123?)\n", i);
  570.        return(0);
  571.     }
  572.  
  573. dice/bcmp                                                          dice/bcmp
  574.  
  575.     FUNCTION
  576.     compare two memory buffers (UNIX)
  577.  
  578.     SYNTAX
  579.     #include <string.h>
  580.     int r = bcmp(s1, s2, bytes)
  581.     void *s1;
  582.     void *s2;
  583.     size_t bytes;
  584.  
  585.     DESCRIPTION
  586.     bcmp compares two memory buffers.  A byte by byte (unsigned)
  587.     comparison is done.  When a comparison fails and the byte in s1 is
  588.     less than the byte in s2 then -1 is returned.  If the byte in s1 is
  589.     greater than the byte in s2 then 1 is returned.  If the count is
  590.     exhausted and all comparisons succeed then 0 is returned indicating
  591.     the two buffers are the same.
  592.  
  593.     INPUTS
  594.     void *s1;        pointer to first buffer
  595.  
  596.     void *s2;        pointer to second buffer
  597.  
  598.     size_t bytes;        size of each buffer
  599.  
  600.     RESULTS
  601.     int r;            -1 if buf s1 < buf s2, 0 if buf s1 == buf s2, 1
  602.                 if buf s1 > buf s2.
  603.  
  604.     SEE ALSO
  605.     cmpmem, memcmp, strcmp
  606.  
  607.     EXAMPLE
  608.     #include <stdlib.h>
  609.     #include <assert.h>
  610.     main()
  611.     {
  612.        unsigned char buf1[] = {5, 12, 13};
  613.        unsigned char buf2[] = {5, 12, 13};
  614.        int r;
  615.        r = bcmp(buf1, buf2, 3);
  616.        assert(r == 0);
  617.  
  618.        buf1[2] = 12;
  619.        r = bcmp(buf1, buf2, 3);
  620.        assert(r < 0);
  621.  
  622.        buf1[2] = 200;
  623.        r = bcmp(buf1, buf2, 3);
  624.        assert(r > 0);
  625.        return(0);
  626.     }
  627.  
  628. dice/c.o                                                            dice/c.o
  629.  
  630.     FUNCTION
  631.     DICE startup module for all C programs
  632.  
  633.     SYNTAX
  634.     The module dlib:c.o is normally specified first in a link line, or
  635.     automatically by dcc.
  636.  
  637.     DESCRIPTION
  638.     C programs require a bit of code (called "glue") in order to start
  639.     execution.  This code saves machine registers, sets up global
  640.     storage, parses arguments, open libraries and, after the program
  641.     terminates, communicates results back to the host Operating System.
  642.     For use with the Amiga, DICE includes a suitable startup module
  643.     called c.o.  This module is automatically include by dcc, and need
  644.     not concern most programmers.
  645.  
  646.     The startup module performs the following tasks:
  647.  
  648.     1) Save non-scratch registers.
  649.  
  650.     2) If resident, allocate space for both DATA (Initialized globals) &
  651.        BSS (Uninitialized globals - set to zero).  Copy initialized data
  652.        into the allocated space. Clear the BSS portion of the data space.
  653.        If the BSS has already been allocated by the load module but not
  654.        cleared, clear the BSS portion of the data space.
  655.  
  656.     3) Clear the ^C (Control C) signal
  657.  
  658.     4) Setup _SysBase, the pointer to the Amiga's master library, Exec.
  659.  
  660.     5) Call all AUTOINIT subroutines (this usually results in at least
  661.        the dos.library being opened).
  662.  
  663.     6) Call _main (usually the c.lib version, but you may override it).
  664.  
  665.     7) Fall through to _exit(0).  Note that while c.a falls through to
  666.        _exit after calling _main, _main itself calls main with:
  667.        exit(main(args...)); Thus, main() is always expected to return a
  668.        valid value (i.e. not void).
  669.  
  670.     C.O. also handles the low-level _exit (__exit:) in the following
  671.     sequence:
  672.  
  673.     1) Call all AUTOEXEC subroutines (this normally closes the DOS
  674.        library and other automatically opened libraries such as floating
  675.        point).
  676.  
  677.     2) Free all memory allocated by the task, including the small data
  678.        segment & BSS space.  Note that all variables we use hereafter
  679.        have already been placed in registers since the dataspace is no
  680.        longer valid.
  681.  
  682.     3) If started from workbench, reply to the workbench mesage, _WBMsg.
  683.  
  684.     4) Restore original registers and rts (exit back to the Operating
  685.        System).
  686.  
  687.     c.o also exists in c.lib, however that version is not normally used.
  688.  
  689.     || NOTE: Normally the programmer does not overide the startup object
  690.     || file (c.o).
  691.  
  692.     However, in some cases a programmer will want to overide _main, as
  693.     in:
  694.  
  695.     _main(len, arg) int len; char *arg; {... }
  696.  
  697.     The _main entry point is passed the length and arg pointer unmodified
  698.     from the OS. When you overide _main you cannot call any stdio (fopen,
  699.     fclose, puts, printf, etc...), any low level IO (open, close, read,
  700.     write, etc...), or any C memory allocation routine (malloc, etc...).
  701.  
  702.     Normally _main will be overridden if the programer wishes to save
  703.     space, and makes only Amiga system calls (such as Open, Close, Read,
  704.     Write, FindTask, etc.). Overriding the C.LIB generally makes
  705.     executables much smaller because no extraneous stdio or low level IO
  706.     routines are brought in from c.lib. Normally you exit out of _main by
  707.     calling _exit(code) (note the underscore).
  708.  
  709. dice/calloc                                                      dice/calloc
  710.  
  711.     FUNCTION
  712.     allocate memory and clear (ANSI)
  713.  
  714.     SYNTAX
  715.     #include <stdlib.h>
  716.     void *ptr = calloc(objsize, numobjs)
  717.     size_t objsize;
  718.     size_t numobjs;
  719.  
  720.     DESCRIPTION
  721.     Numobjs objects each objsize in size are allocated contiguously and a
  722.     pointer to the first object is returned.  The memory is cleared to 0.
  723.     Effectively this is equivalent to malloc(objsize * numobjs), and then
  724.     setmem(ptr, objsize * numobjs, 0) . calloc returns NULL if the memory
  725.     cannot be allocated.
  726.  
  727.     INPUTS
  728.     size_t objsize;     size of each object
  729.  
  730.     size_t numobjs;     number of objects to allocate
  731.  
  732.     RESULTS
  733.     void *ptr;        pointer to first object
  734.  
  735.     SEE ALSO
  736.     malloc, strdup
  737.  
  738.     EXAMPLE
  739.     /*
  740.      *  allocate 16 objects and fill with junk
  741.      */
  742.     #include <stdlib.h>
  743.     #include <assert.h>
  744.     typedef struct
  745.     {
  746.        long a, b, c;
  747.     }
  748.     Junk;
  749.     main()
  750.     {
  751.        Junk *jp;
  752.        jp = calloc(sizeof(Junk), 16);
  753.        assert(jp);
  754.        {
  755.           Junk *tj = jp;
  756.           short i;
  757.           for (i = 0; i < 16; ++i, ++tj)
  758.           {
  759.          tj->a = 1;
  760.          tj->b = 2;
  761.          tj->c = 3;
  762.           }
  763.        }
  764.     free(jp);
  765.     return(0);
  766.     }
  767.  
  768. dice/chdir                                                        dice/chdir
  769.  
  770.     FUNCTION
  771.     change current directory (UNIX)
  772.  
  773.     SYNTAX
  774.     #include <stdio.h>
  775.     int r = chdir(path);
  776.     const char *path;
  777.  
  778.     DESCRIPTION
  779.     chdir changes the current directory to the specified path, returning
  780.     0 on success and -1 on failure.
  781.  
  782.     || NOTE: When a program exits, the original directory will be
  783.     || restored.
  784.  
  785.     INPUTS
  786.     char *path;        path to chdir into
  787.  
  788.     RESULTS
  789.     int r;            return value, 0 if ok, -1 if error
  790.  
  791.     SEE ALSO
  792.     getcwd
  793.  
  794.     EXAMPLE
  795.     #include <stdio.h>
  796.     char buf[512];
  797.     main(int ac, char **av)
  798.     {
  799.        getcwd(buf, sizeof(buf));
  800.        if (chdir("RAM:")) {
  801.           puts("Couldn't chdir into RAM:");
  802.           exit(1);
  803.        }
  804.        FILE *fp;
  805.        if (fp = fopen("yy", "w")) {
  806.           fputs("Fight for vegtable rights!\n", fp);
  807.           fclose(fp);
  808.           puts("created file yy in RAM:");
  809.        }
  810.        if (chdir(buf)) {
  811.           printf("Unable to chdir back into %s\n", buf);
  812.        }
  813.        return(0);
  814.     }
  815.  
  816. dice/chkabort                                                  dice/chkabort
  817.  
  818.     FUNCTION
  819.     Check for ^C and take the appropriate action (AmigaDOS)
  820.  
  821.     SYNTAX
  822.     (void) chkabort(void);
  823.  
  824.     DESCRIPTION
  825.     chkabort checks for a ^C and takes the appropriate action.  If the
  826.     appropriate action is to exit then this routine does not return.
  827.     Stdio and other routines will call chkabort at various points.    The
  828.     action taken by ^C may be set by the signal or onbreak calls.
  829.  
  830.     SEE ALSO
  831.     onbreak, atexit, signal
  832.  
  833.     EXAMPLE
  834.     /*
  835.      *  wait for somebody to hit ^C (note that this is very
  836.      *  wasteful of CPU and thus isn't a real good example).
  837.      */
  838.     main()
  839.     {
  840.        int i;
  841.        for (i = 0; i < 10000000; ++i)
  842.           chkabort();
  843.        return(0);
  844.     }
  845.  
  846. dice/clearerr                                                  dice/clearerr
  847.  
  848.     FUNCTION
  849.     Clear error associated with a file pointer (ANSI)
  850.  
  851.     SYNTAX
  852.     #include <stdio.h>
  853.     void clearerr(fp);
  854.     (MACRO) FILE *fp;
  855.  
  856.     DESCRIPTION
  857.     The clearerr macro clears both the EOF flag and the ERROR flag
  858.     associated with a file pointer. When an ERROR occurs on a file
  859.     pointer, addition fread, fwrite, etc. calls will not work until the
  860.     ERROR indicator is cleared.
  861.  
  862.     || NOTE: Refer to the file_pointer manual page for general
  863.     || information.
  864.  
  865.     INPUTS
  866.     FILE *fp;        file pointer to clear the error on.
  867.  
  868.     RESULTS
  869.     none;            the error and EOF indicators are cleared
  870.  
  871.     SEE ALSO
  872.     feof, ferror, rewind, fseek
  873.  
  874. dice/clock                                                        dice/clock
  875.  
  876.     FUNCTION
  877.     return system clock value (ANSI)
  878.  
  879.     SYNTAX
  880.     #include <time.h>
  881.     clock_t clk = clock(void);
  882.  
  883.     DESCRIPTION
  884.     clock returns the system clock in ticks.  To obtain seconds from
  885.     ticks divide the returned value by CLK_TCK in <time.h>.
  886.  
  887.     INPUTS
  888.  
  889.     none
  890.  
  891.     RESULTS
  892.     clock_t clk;        system clock time value
  893.  
  894.     SEE ALSO
  895.     time, localtime, asctime, strftime, ctime, clock
  896.  
  897.     EXAMPLE
  898.     #include <stdio.h>
  899.     #include <time.h>
  900.  
  901.     main()
  902.     {
  903.        clock_t clk = clock();
  904.        long i;
  905.        clk = clk + CLK_TCK;
  906.        for (i = 0; clk - clock() > 0; ++i)
  907.           ;
  908.        printf("The FOR loop calling clock() took\
  909.            %d loops in one second\n", i);
  910.        return(0);
  911.     }
  912.  
  913. dice/close                                                        dice/close
  914.  
  915.     FUNCTION
  916.     close a file descriptor (UNIX)
  917.  
  918.     SYNTAX
  919.     #include <fcntl.h>
  920.     int r = close(fd);
  921.     int fd;
  922.  
  923.     DESCRIPTION
  924.     close closes a file descriptor.  If an error occurs or the descriptor
  925.     is invalid, a non-zero return code will result and errno will be set
  926.     to the appropriate error condition.
  927.  
  928.     || NOTE: Refer to the file_descriptor manual page for general
  929.     || information.  Unlike file pointers and file handles, the file
  930.     || descriptor is checked for validity and if illegal, an error will
  931.     || be returned.
  932.  
  933.     INPUTS
  934.     int fd;         file descriptor to close, the file descriptor
  935.                 becomes invalid after this call
  936.  
  937.     RESULTS
  938.     int r;            return value, 0 == ok, non-zero == error
  939.  
  940.     SEE ALSO
  941.     creat, fcntl, fdtofh, ioctl, isatty, lseek, mkdir, open, read, rmdir,
  942.     unlink, write
  943.  
  944.     See open for an example
  945.  
  946. dice/cmpmem                                                      dice/cmpmem
  947.  
  948.     FUNCTION
  949.     compare two memory buffers (UNIX)
  950.  
  951.     SYNTAX
  952.     #include <string.h>
  953.     int r = cmpmem(s1, s2, bytes)
  954.     void *s1;
  955.     void *s2;
  956.     size_t bytes;
  957.  
  958.     DESCRIPTION
  959.     Like bcmp, this function compares two memory buffers.  A byte by byte
  960.     (unsigned) comparison is done.    When a comparison fails and the byte
  961.     in s1 is less than the byte in s2 then -1 is returned. If the byte in
  962.     s1 is greater than the byte in s2 then 1 is returned. If the count is
  963.     exhausted and all comparisons succeed then 0 is returned indicating
  964.     the two buffers are the same.
  965.  
  966.     INPUTS
  967.     void *s1;        pointer to first buffer
  968.  
  969.     void *s2;        pointer to second buffer
  970.  
  971.     size_t bytes;        size of each buffer
  972.  
  973.     RESULTS
  974.     int r;            -1 if buf s1 < buf s2, 0 if buf s1 == buf s2, 1
  975.                 if buf s1 > buf s2.
  976.  
  977.     SEE ALSO
  978.     bcmp, memcmp
  979.  
  980.     EXAMPLE
  981.     #include <stdlib.h>
  982.     #include <assert.h>
  983.     main()
  984.     {
  985.        unsigned char buf1[] = {3, 5, 7, 11, 13};
  986.        unsigned char buf2[] = {3, 5, 7, 11, 13};
  987.        int r;
  988.  
  989.        r = cmpmem(buf1, buf2, 5);
  990.        assert(r == 0);
  991.        buf1[2] = 2;
  992.        r = cmpmem(buf1, buf2, 5);
  993.        assert(r < 0);
  994.        buf1[2] = 200;
  995.        r = cmpmem(buf1, buf2, 5);
  996.        assert(r > 0);
  997.        return(0);
  998.     }
  999.  
  1000. dice/cos,fcos                                                  dice/cos,fcos
  1001.  
  1002.     FUNCTION
  1003.     cos: return cosine of a double (ANSI)
  1004.     fcos: return cosine of a float (ANSI)
  1005.  
  1006.     LIBRARY
  1007.     m.lib
  1008.  
  1009.     SYNTAX
  1010.     #include <math.h>
  1011.     double a = cos(b);
  1012.     double c;
  1013.     float  c = fcos(d);
  1014.     double d;
  1015.  
  1016.     DESCRIPTION
  1017.     cos returns the cosine of a type double quantity. fcos is the same,
  1018.     except expexts a type float.  These functions use radian measure.
  1019.  
  1020.     INPUTS
  1021.     double b;        double floating point value
  1022.  
  1023.     RESULTS
  1024.     double a;        result double floating point value
  1025.  
  1026.     SEE ALSO
  1027.     acos, asin, atan, exp, fabs, log, log10, pow, sin, sqrt, tan facos,
  1028.     fasin, fcos
  1029.  
  1030.     EXAMPLE
  1031.     /*
  1032.      *  compile with the math library -lm
  1033.      */
  1034.     #include <math.h>
  1035.     #include <stdio.h>
  1036.  
  1037.     main()
  1038.     {
  1039.        {
  1040.           double a = cos(0.25);
  1041.           printf("cos 0.25 = %lf\n", a);  /* 0.9689 */
  1042.        }
  1043.        {  /*  less accuracy   */
  1044.           float a = fcos(0.25);
  1045.           printf("cos 0.25 = %lf\n", (double)a);
  1046.        }
  1047.        return(0);
  1048.     }
  1049.  
  1050. dice/creat                                                        dice/creat
  1051.  
  1052.     FUNCTION
  1053.     create a file (UNIX)
  1054.  
  1055.     SYNTAX
  1056.     #include <fnctl.h>
  1057.     int fd = creat(file);
  1058.     char *file;
  1059.  
  1060.     DESCRIPTION
  1061.     creat creates a new file and returns a file descriptor for it. This
  1062.     call is equivalent to open(file,O_CREAT|O_TRUNC|O_RDWR); this is an
  1063.     obsolete function and should not be used.
  1064.  
  1065. dice/ctime                                                        dice/ctime
  1066.  
  1067.     FUNCTION
  1068.     convert time into standard text (ANSI)
  1069.  
  1070.     SYNTAX
  1071.     #include <time.h>
  1072.     char *str = ctime(&t); time_t t;
  1073.  
  1074.     DESCRIPTION
  1075.     ctime converts a time pointer into ASCII text using the following
  1076.      format:
  1077.         Sun Dec 8 01:53:33 1987\n\0
  1078.     where \n stands for a newline character and \0 is terminating nul.
  1079.     The string is stored in a static buffer shared by both asctime and
  1080.     ctime and so will be overwritten whenever either function is called.
  1081.  
  1082.     INPUTS
  1083.     time_t *t;        pointer to a time_t value
  1084.  
  1085.     RESULTS
  1086.     char *str;        pointer to static string
  1087.  
  1088.     SEE ALSO
  1089.     time, localtime, asctime, strftime, ctime, clock
  1090.  
  1091.     EXAMPLE
  1092.     #include <stdio.h>
  1093.     #include <time.h>
  1094.  
  1095.     main()
  1096.     {
  1097.        time_t t = time(NULL);
  1098.        fputs(ctime(&t), stdout);
  1099.        return(0);
  1100.     }
  1101.  
  1102. dice/dir                                                            dice/dir
  1103.  
  1104.     FUNCTION
  1105.     disk directory scanning (UNIX)
  1106.  
  1107.     SYNTAX
  1108.     #include <sys/dir.h>
  1109.     DIR *dirhan = opendir(path);
  1110.     struct direct *entry = readdir(dirhan);
  1111.     (void) rewinddir(dirhan);
  1112.     void
  1113.     closedir(dirhan);
  1114.     const char *path;
  1115.     DIR *dirhan;
  1116.  
  1117.     DESCRIPTION
  1118.     These are UNIX compatible directory scanning calls.  After opening a
  1119.     directory with opendir, you may scan it with successive calls to
  1120.     readdir until NULL is returned, then either rewinddir it for a
  1121.     rescan, or closedir it when done.  The DIR structure is private to
  1122.     the library.  Valid fields within struct direct are d_name (the file
  1123.     name), and d_namlen (the length of the file name, not usually
  1124.     needed).  You can chdir into the directory and stat each entry to
  1125.     obtain additional information.    Note that while the UNIX directory
  1126.     scanning routines will not be as efficient as the Amiga directory
  1127.     scanning routines, the UNIX directory scanning routines are portable.
  1128.  
  1129.     || NOTE: Unlike the Amiga directory scanning routines that use Amiga
  1130.     || File Locks, these calls will automatically deallocate resources if
  1131.     || the program terminates. rewinddir's prototype returns an int.
  1132.     || This is for internal use only.  You should never use rewinddir's
  1133.     || return value yourself.
  1134.  
  1135.     SEE ALSO
  1136.     chdir
  1137.  
  1138.     EXAMPLE
  1139.     #include <stdio.h>
  1140.     #include <sys/dir.h>
  1141.     main(int ac, char*av[])
  1142.     {
  1143.        DIR *dir;
  1144.        if (ac == 1)
  1145.        {
  1146.           puts("test dir");
  1147.           exit(1);
  1148.        }
  1149.        if (dir = opendir(av[1]))
  1150.        {
  1151.           struct direct *entry;
  1152.           while (entry = readdir(dir))
  1153.           {
  1154.          printf("%s\n", entry->d_name);
  1155.           }
  1156.           closedir(dir);
  1157.        }
  1158.        return(0);
  1159.     }
  1160.  
  1161. dice/_divs,_divu                                            dice/_divs,_divu
  1162.  
  1163.     FUNCTION
  1164.     Signed and unsigned long divide (DICE)
  1165.  
  1166.     DESCRIPTION
  1167.     DICE uses these assembly level functions whenever it needs to do long
  1168.     division; they are not callable from C.
  1169.  
  1170.     INPUTS
  1171.     D0            32 bit signed/unsigned integer
  1172.  
  1173.     D1            32 bit signed/unsigned integer
  1174.  
  1175.     RESULTS
  1176.     D0            D0 divided by D1
  1177.  
  1178.     SEE ALSO
  1179.     _mods, _modu, _muls, _mulu
  1180.  
  1181. dice/exit                                                          dice/exit
  1182.  
  1183.     FUNCTION
  1184.     Exit from a program 'nicely' (ANSI)
  1185.  
  1186.     SYNTAX
  1187.     #include <stdlib.h>
  1188.     (void) exit(code)
  1189.  
  1190.     DESCRIPTION
  1191.     exit exits the program and returns the specified exit code. Normally
  1192.     you pass 0 to indicate no errors, and a positive number to indicate a
  1193.     program error to the parent.  exit closes all stdio file pointers,
  1194.     low level file descriptors, perhaps a few other things, and then
  1195.     finally calls _exit with the code. If you use main you should call
  1196.     exit to exit the program or return an error code from main.  If you
  1197.     use the _main entry point (only for programmers dead set on
  1198.     optimizing executable size and using only system library calls) you
  1199.     should use the _exit exit point.
  1200.  
  1201.     SEE ALSO
  1202.     main, _main, _exit
  1203.  
  1204.     EXAMPLE
  1205.     main(int ac, char *av[])
  1206.     {
  1207.        if (ac <= 1) {
  1208.           puts("Sorry, you must supply a parameter!");
  1209.           exit(1);
  1210.        }
  1211.        puts("Thanks!");
  1212.        exit(0);
  1213.     }
  1214.  
  1215. dice/_exit                                                        dice/_exit
  1216.  
  1217.     FUNCTION
  1218.     exit from a program without bothering to release resources (ANSI)
  1219.  
  1220.     SYNTAX
  1221.     #include <stdlib.h>
  1222.     (void) _exit(code)
  1223.     int code;
  1224.  
  1225.     DESCRIPTION
  1226.     _exit exits from a program and returns the specified exit code.
  1227.     Normally you pass 0 to indicate no errors, a positive number to
  1228.     indicate a program error to the parent.  Note that since auto-init
  1229.     opened libraries are closed in the startup module (c.o),
  1230.     automatically opened libraries will be automatically closed for you.
  1231.     However, any libraries you manually declare the library base variable
  1232.     for and manually open must be closed by you.  You should only call
  1233.     _exit if you used the _main entry point (instead of the usual main),
  1234.     and then only after releasing all resources (such as file handles
  1235.     opened with open).
  1236.  
  1237.     INPUTS
  1238.     code            int;  code is a value that is passed back to
  1239.                 caller
  1240.  
  1241.     SEE ALSO
  1242.     main, _main, exit
  1243.  
  1244.     EXAMPLE
  1245.     _main()
  1246.     {
  1247.        Write(Output(), "Ouch!!\n", 7);
  1248.        _exit(20); /* 20 - Severe Error */
  1249.     }
  1250.  
  1251. dice/exp                                                            dice/exp
  1252.  
  1253.     FUNCTION
  1254.     return e to the power of the double quantity (ANSI)
  1255.  
  1256.     LIBRARY
  1257.     m.lib
  1258.  
  1259.     SYNTAX
  1260.     #include <math.h>
  1261.     double a = exp(b);
  1262.     double b;
  1263.  
  1264.     DESCRIPTION
  1265.     exp returns e to the power of the floating point quantity.
  1266.  
  1267.     INPUTS
  1268.     double b;        double floating point value
  1269.  
  1270.     RESULTS
  1271.     double a;        result double floating point value
  1272.  
  1273.     SEE ALSO
  1274.     acos, asin, atan, cos, fabs, log, log10, pow, sin,  sqrt, tan facos,
  1275.     fasin
  1276.  
  1277.     EXAMPLE
  1278.     /*
  1279.      *  compile with the math library -lm
  1280.      */
  1281.     #include <math.h>
  1282.     #include <stdio.h>
  1283.     main()
  1284.     {
  1285.        {
  1286.           double a = exp(0.25);
  1287.           printf("exp 0.25 = %lf\n", a);
  1288.        }
  1289.        {  /*  less accuracy   */
  1290.           float a = fexp(0.25);
  1291.           printf("exp 0.25 = %lf\n", (double)a);
  1292.        }
  1293.        return(0);
  1294.     }
  1295.  
  1296. dice/expand_args                                            dice/expand_args
  1297.  
  1298.     FUNCTION
  1299.     expand command line argument wildcards (DICE)
  1300.  
  1301.     SYNTAX
  1302.     #include <stdio.h>
  1303.     int error = expand_args(xac, xav, &ac, &av);
  1304.     int xac;
  1305.     const char **xav;
  1306.     int ac;
  1307.     char **av;
  1308.  
  1309.     DESCRIPTION
  1310.     expand_args is a powerful and convenient function.  A few lines of
  1311.     simple code allow your program to have full wildcard support under
  1312.     any version of the OS.    expand_args takes an argc/argv list and
  1313.     expands any wildcard arguments by scanning the appropriate directory.
  1314.     It  malloc's however much memory it needs to create the new list and
  1315.     ignores xav[0] (that is, it just copies it to the returned av[0]
  1316.     without doing a wildcard expansion). expand_args fills in the ac and
  1317.     av variables with its own malloc'd version of the argument list, now
  1318.     completely expanded.  There is no limit to the number of files that
  1319.     may be in this result list (you could conceivably have thousands).
  1320.     expand_args may be used to expand arbitrary AmigaDOS wildcards and is
  1321.     not limited to an anchored search.  For example, you could specify:
  1322.     sys:#?/#? in which case a list of a second level files/dirs will be
  1323.     generated. In the above case, expand_args scans sys, then scans any
  1324.     sub directories found in sys.  Generic AmigaDOS wildcarding is used
  1325.     and incredibly complex wildcards may be specified.  Please note,
  1326.     however, that any wildcard elements containing #? in combination with
  1327.     other elements (such as (a|b|c)) will cause huge amounts of stack to
  1328.     be used and also quite a bit of memory during the scan. expand_args
  1329.     limits itself to 4K of stack before giving up. Any program that uses
  1330.     expand_args should be run with at least 8K of stack.
  1331.  
  1332.     INPUTS
  1333.     int  xac;        original argc
  1334.  
  1335.     char **xav;        original argv
  1336.  
  1337.     int  *ac;        pointer to new argc
  1338.  
  1339.     char ***av;        pointer to new argv
  1340.  
  1341.     RESULTS
  1342.     int error;        0 if all went well, non-zero otherwise
  1343.  
  1344.     EXAMPLE
  1345.     #include <stdio.h>
  1346.     main(int xac, char **xav)
  1347.     {
  1348.        int ac, i, error;
  1349.        char **av;
  1350.        int error = expand_args(xac, xav, &ac, &av);
  1351.        for (i = 1; i < ac; ++i) {
  1352.           printf("%s\n", av[i]);
  1353.        }
  1354.     }    /* expand_args #?.c */
  1355.  
  1356. dice/fabs                                                          dice/fabs
  1357.  
  1358.     FUNCTION
  1359.     return the absolute value of a double quantity (ANSI)
  1360.  
  1361.     LIBRARY
  1362.     m.lib
  1363.  
  1364.     SYNTAX
  1365.     #include <math.h>
  1366.     double a = fabs(b);
  1367.     double b;
  1368.  
  1369.     DESCRIPTION
  1370.     fabs returns the absolute value of a floating point quantity.
  1371.  
  1372.     INPUTS
  1373.     float d;        float floating point value
  1374.  
  1375.     RESULTS
  1376.     int arg;        control argument
  1377.  
  1378.     int r;            result, error if less than 0.
  1379.  
  1380.     EXAMPLE
  1381.     /*
  1382.      *  compile with the math library -lm
  1383.      */
  1384.     #include <math.h>
  1385.     #include <stdio.h>
  1386.     main()
  1387.     {
  1388.        {
  1389.           double a = fabs(-0.25);
  1390.           printf("fabs -0.25 = %lf\n", a); /*  0.25  */
  1391.        }
  1392.        {  /*  less accuracy   */
  1393.           float a = ffabs(-0.25);
  1394.           printf("fabs -0.25 = %lf\n", (double)a);
  1395.        }
  1396.        return(0);
  1397.     }
  1398.  
  1399. dice/facos                                                        dice/facos
  1400.  
  1401.     FUNCTON
  1402.     float arc cosine (UNIX)
  1403.  
  1404.     LIBRARY
  1405.     m.lib
  1406.  
  1407.     SYNTAX
  1408.     #include <math.h>
  1409.     float  c = facos(d);
  1410.     float  d;
  1411.  
  1412.     DESCRIPTION
  1413.     facos returns the arc cosine of a floating point quantity.
  1414.  
  1415.     INPUTS
  1416.     float d;        float floating point value
  1417.  
  1418.     RESULTS
  1419.     result float;        floating point value
  1420.  
  1421.     SEE ALSO
  1422.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan,
  1423.     fasin
  1424.  
  1425.     EXAMPLE
  1426.     /*
  1427.      *  compile with the math library -lm
  1428.      */
  1429.     #include <math.h>
  1430.     #include <stdio.h>
  1431.     main()
  1432.     {
  1433.        {
  1434.            double a = acos(0.25);
  1435.            printf("acos 0.25 = %lf\n", a); /* 1.318   */
  1436.        }
  1437.        {   /* less accuracy   */
  1438.            float a = facos(0.25);
  1439.            printf("acos 0.25 = %lf\n", (double)a);
  1440.        }
  1441.        return(0);
  1442.     }
  1443.  
  1444. dice/fasin                                                        dice/fasin
  1445.  
  1446.     FUNCTION
  1447.     return arc sine of a float quantity (UNIX)
  1448.  
  1449.     LIBRARY
  1450.     m.lib
  1451.  
  1452.     SYNTAX
  1453.     #include <math.h>
  1454.     float  c = fasin(d);
  1455.     float  d;
  1456.  
  1457.     DESCRIPTION
  1458.     fasin returns the arc sine of a floating point quantity.
  1459.  
  1460.     INPUTS
  1461.     float d;        float floating point value
  1462.  
  1463.     RESULTS
  1464.     float c;        result float loating point value
  1465.  
  1466.     SEE ALSO
  1467.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan
  1468.     facos
  1469.  
  1470.     EXAMPLE
  1471.     /*
  1472.      *  compile with the math library -lm
  1473.      */
  1474.     #include <math.h>
  1475.     #include <stdio.h>
  1476.  
  1477.     main()
  1478.     {
  1479.        {
  1480.           double a = asin(0.25);
  1481.           printf("asin 0.25 = %lf\n", a);  /* 0.2527 */
  1482.        }
  1483.        {  /*  less accuracy */
  1484.           float a = fasin(0.25);
  1485.           printf("asin 0.25 = %lf\n", (double)a);
  1486.        }
  1487.        return(0);
  1488.     }
  1489.  
  1490. dice/fatan                                                        dice/fatan
  1491.  
  1492.     FUNCTION
  1493.     return arc tan of a float quantity (UNIX)
  1494.  
  1495.     LIBRARY
  1496.     m.lib
  1497.  
  1498.     SYNTAX
  1499.     #include <math.h>
  1500.     float  c = fatan(d); float  d;
  1501.  
  1502.     DESCRIPTION
  1503.     fatan returns the arc tan of a floating point quantity.
  1504.  
  1505.     INPUTS
  1506.     float d;        float floating point value
  1507.  
  1508.     RESULTS
  1509.     float c;        result float floating point value
  1510.  
  1511.     SEE ALSO
  1512.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan
  1513.     facos, fasin
  1514.  
  1515.     EXAMPLE
  1516.     /*
  1517.      *  compile with the math library -lm
  1518.      */
  1519.     #include <math.h>
  1520.     #include <stdio.h>
  1521.     main()
  1522.     {
  1523.        {
  1524.           double a = atan(0.25);
  1525.           printf("atan 0.25 = %lf\n", a);  /* 0.245   */
  1526.        }
  1527.        {  /* less accuracy     */
  1528.           float a = fatan(0.25);
  1529.           printf("atan 0.25 = %lf\n", (double)a);
  1530.        }
  1531.        return(0);
  1532.     }
  1533.  
  1534. dice/fclose                                                      dice/fclose
  1535.  
  1536.     FUNCTION
  1537.     close a file pointer (ANSI)
  1538.  
  1539.     SYNTAX
  1540.     #include <stdio.h>
  1541.     int error = fclose(fp);
  1542.     FILE    *fp;
  1543.  
  1544.     DESCRIPTION
  1545.     fclose flushes any data remaining in the file pointer's output buffer
  1546.     to the file and then closes the file. The file pointer is no longer
  1547.     valid. fclose returns any error condition that occured while it was
  1548.     flushing the buffered data to the file.  The file is closed even if
  1549.     an error occured.
  1550.  
  1551.     || NOTE: You can fclose(stdin), fclose(stdout), and fclose(stderr) to
  1552.     || save space or detach your process from the console (i.e. allow the
  1553.     || console window to be closed).
  1554.  
  1555.     ## WARNING: If you fclose stdin, stdout, and stderr with the
  1556.     ## intention of removing all references to the console window, you
  1557.     ## must put a NULL in your processes pr_ConsoleTask field. Otherwise,
  1558.     ## the console window will be able to close, but your process will
  1559.     ## still have a reference to the now non-existent window.  Refer to
  1560.     ## the file_pointer manual page for general information
  1561.  
  1562.     INPUTS
  1563.     FILE *F;        file pointer
  1564.  
  1565.     RESULTS
  1566.     int error;        error on fclose, or 0 if none
  1567.  
  1568.     SEE ALSO
  1569.     fopen, fread, fwrite, fgets, fputs
  1570.  
  1571. dice/fcntl                                                        dice/fcntl
  1572.  
  1573.     FUNCTION
  1574.     file control on a file (UNIX)
  1575.  
  1576.     SYNTAX
  1577.     #include <fcntl.h>
  1578.     int r = fcntl(fd, req, arg)
  1579.     int fd;
  1580.     int req;
  1581.     int arg;
  1582.  
  1583.     DESCRIPTION
  1584.     fcntl may be used to control various aspects of an fd and is a higher
  1585.     level call than ioctl.    Currently, nothing truly significant can be
  1586.     accomplished by a fcntl call for files.  However, fcntl fully
  1587.     supports programmer simulated file descriptors.
  1588.  
  1589.     || NOTE: Refer to the file_descriptor manual page for general
  1590.     || information Unlike file pointers and file handles, the file
  1591.     || descriptor is checked for validity and if illegal, an error will
  1592.     || be returned.
  1593.  
  1594.     INPUTS
  1595.     int fd;         file descriptor to operate on
  1596.  
  1597.     int req;        request from <fcntl.h> (F_* defines)
  1598.  
  1599.     int arg;        control argument
  1600.  
  1601.     RESULTS
  1602.     int r;            result, error if less than 0
  1603.  
  1604.     SEE ALSO
  1605.     close, creat, fcntl, fdtofh, ioctl, isatty, lseek, mkdir, open, read,
  1606.     rmdir, unlink, write
  1607.  
  1608. dice/fdopen                                                      dice/fdopen
  1609.  
  1610.     FUNCTION
  1611.     associate a file pointer with an open file descriptor (UNIX)
  1612.  
  1613.     SYNTAX
  1614.     #include <stdio.h>
  1615.     FILE *fp = fdopen(fd, modes);
  1616.     int fd; char *modes;
  1617.  
  1618.     DESCRIPTION
  1619.     fdopen associates an open file descriptor with a file pointer.
  1620.  
  1621.     || NOTE: Once fclose is used, the file pointer will also close the
  1622.     || file descriptor.  Refer to the fopen manual page for a description
  1623.     || of available modes. Also, when you use fdopen the file will not be
  1624.     || truncated and if you specify mode a for append, the file
  1625.     || descriptor must have been opened with the O_APPEND flag.  That is,
  1626.     || the mode string should be similar to the open flags that were used
  1627.     || to open the file descriptor. Refer to the file_pointer manual page
  1628.     || for general information
  1629.  
  1630.     INPUTS
  1631.     int fd;         file descriptor to associated with a new file
  1632.                 pointer
  1633.  
  1634.     char *modes;        modes string, such as "r+".
  1635.  
  1636.     RESULTS
  1637.     FILE *fp;        new file pointer or NULL if an error occured
  1638.  
  1639.     SEE ALSO
  1640.     fopen, fread, fwrite, fgets, fputs
  1641.  
  1642. dice/fdtofh                                                      dice/fdtofh
  1643.  
  1644.     FUNCTION
  1645.     return AmigaDOS file handle for file descriptor (AmigaDOS)
  1646.  
  1647.     SYNTAX
  1648.     #include <stdio.h>
  1649.     BPTR fh = fdtofh(fd);
  1650.     int fd;
  1651.  
  1652.     DESCRIPTION
  1653.     fdtofh returns the AmigaDOS file handle associated with a file
  1654.     descriptor or NULL if the file descriptor is illegal or simulated.
  1655.     You may then make AmigaDOS library calls using the file handle.
  1656.  
  1657.     INPUTS
  1658.     int fd;         file descriptor
  1659.  
  1660.     RESULTS
  1661.     BPTR fh;        associated file handle or NULL
  1662.  
  1663.     SEE ALSO
  1664.     close, creat, fcntl, ioctl, isatty, lseek, mkdir, open, read, rmdir,
  1665.     unlink, write
  1666.  
  1667.     EXAMPLE
  1668.     #include <stdio.h>
  1669.     main()
  1670.     {
  1671.     FILE * fh;
  1672.        write(1, "Quayle\n", 6);
  1673.        if( fh = fdtofh(1) )  /* Example Only! */
  1674.           Write(fh, "Bait\n", 6);
  1675.        return(0);
  1676.     }
  1677.  
  1678. dice/feof                                                          dice/feof
  1679.  
  1680.     FUNCTION
  1681.     test a file pointer for End Of File (ANSI)
  1682.  
  1683.     SYNTAX
  1684.     #include <stdio.h>
  1685.     int r = feof(fp);
  1686.     (MACRO) FILE *fp;
  1687.  
  1688.     DESCRIPTION
  1689.     feof returns the EOF  status of a file pointer.  The status is not
  1690.     changed by this macro.    0 is returned if no EOF condition exists,
  1691.     non-zero if an EOF condition exists (not necessarily 1 or -1, just
  1692.     non-zero).  Use clearerr to clear the EOF condition. Note than fseek
  1693.     and rewind also clear an EOF condition.
  1694.  
  1695.     INPUTS
  1696.     FILE *fp;        file pointer
  1697.  
  1698.     RESULTS
  1699.     int r;            0 if no EOF condition exists, != 0 if an EOF
  1700.                 condition exists (not necessarily 1 or -1).
  1701.  
  1702.     SEE ALSO
  1703.     fopen, fclose, fread, fwrite, fgets, fputs, file_pointer
  1704.  
  1705. dice/ferror                                                      dice/ferror
  1706.  
  1707.     FUNCTION
  1708.     return ERROR condition for file pointer (ANSI)
  1709.  
  1710.     SYNTAX
  1711.     #include <stdio.h>
  1712.     int r = ferror(fp); /* MACRO */
  1713.     FILE *fp;
  1714.  
  1715.     DESCRIPTION
  1716.     ferror returns the ERROR status of a file pointer.  The status is not
  1717.     changed by this macro.    0 is returned if no ERROR condition exists,
  1718.     non-zero if an ERROR condition exists (not necessarily 1 or -1, just
  1719.     non-zero).
  1720.  
  1721.     INPUTS
  1722.     FILE *fp;        file pointer
  1723.  
  1724.     RESULTS
  1725.     int r;            0 if no ERROR condition exists, != 0 if an ERROR
  1726.                 condition exists (not necessarily 1 or -1).
  1727.  
  1728.     SEE ALSO
  1729.     fopen, fclose, fread, fwrite, fgets, fputs, file_pointer
  1730.  
  1731. dice/fexp                                                          dice/fexp
  1732.  
  1733.     FUNCTION
  1734.     return e to the power of the float quantity (UNIX)
  1735.  
  1736.     LIBRARY
  1737.     m.lib
  1738.  
  1739.     SYNTAX
  1740.     #include <math.h>
  1741.     float  c = fexp(d);
  1742.     float  d;
  1743.  
  1744.     DESCRIPTION
  1745.     fexp returns e to the power of the floating point quantity
  1746.  
  1747.     INPUTS
  1748.     float d;        float floating point value
  1749.  
  1750.     RESULTS
  1751.     float c;        result float  floating point value
  1752.  
  1753.     SEE ALSO
  1754.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan,
  1755.     facos, fasin
  1756.  
  1757.     EXAMPLE
  1758.     /*
  1759.      *  compile with the math library -lm
  1760.      */
  1761.     #include <math.h>
  1762.     #include <stdio.h>
  1763.  
  1764.     main()
  1765.     {
  1766.        {
  1767.           double a = exp(0.25);
  1768.           printf("exp 0.25 = %lf\n", a);
  1769.           printf("exp 0    = %lf\n", (double)exp(0) );
  1770.        }
  1771.        {  /*  less accuracy   */
  1772.           float a = fexp(0.25);
  1773.           printf("exp 0.25 = %lf\n", (double)a);
  1774.        }
  1775.        return(0);
  1776.     }
  1777.  
  1778. dice/ffabs                                                        dice/ffabs
  1779.  
  1780.     FUNCTION
  1781.     return the absolute value of a float (UNIX)
  1782.  
  1783.     LIBRARY
  1784.     m.lib
  1785.  
  1786.     SYNTAX
  1787.     #include <math.h>
  1788.     float  c = ffabs(d);
  1789.     float  d;
  1790.  
  1791.     DESCRIPTION
  1792.     ffabs returns the absolute value of a floating point quantity.
  1793.  
  1794.     INPUTS
  1795.     float d;        float floating point value
  1796.  
  1797.     RESULTS
  1798.     int arg;        control argument
  1799.  
  1800.     int r;            result, error if less than 0.
  1801.  
  1802.     SEE ALSO
  1803.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan,
  1804.     facos, fasin
  1805.  
  1806.     EXAMPLE
  1807.     /*
  1808.      *  compile with the math library -lm
  1809.      */
  1810.     #include <math.h>
  1811.     #include <stdio.h>
  1812.     main()
  1813.     {
  1814.        {
  1815.            double a = fabs(-0.25);
  1816.            printf("fabs -0.25 = %lf\n", a); /*  0.25  */
  1817.        }
  1818.        {  /*  less accuracy   */
  1819.           float a = ffabs(-0.25);
  1820.           printf("fabs -0.25 = %lf\n", (double)a);
  1821.        }
  1822.        return(0);
  1823.     }
  1824.  
  1825. dice/fflush                                                      dice/fflush
  1826.  
  1827.     FUNCTION
  1828.     flush buffers to file (ANSI)
  1829.  
  1830.     SYNTAX
  1831.     #include <stdio.h>
  1832.     int error = fflush(fp);
  1833.     FILE *fp;
  1834.  
  1835.     DESCRIPTION
  1836.     fflush writes out any buffered data out to the file descriptor
  1837.     associated with the file pointer. Normally a file is either
  1838.     unbuffered, line buffered, or fully buffered;  fflush is useful in
  1839.     the latter two cases as is shown by the example. The function will
  1840.     return -1 if a write error occured, 0 if no error occured.
  1841.  
  1842.     || NOTE: Refer to the file_pointer manual page for general
  1843.     || information.
  1844.  
  1845.     INPUTS
  1846.     FILE *fp;        file pointer
  1847.  
  1848.     RESULTS
  1849.     int  error;        0 if no error, -1 on error.
  1850.  
  1851.     SEE ALSO
  1852.     fopen, fclose, fread, fwrite, fgets, fputs
  1853.  
  1854.     EXAMPLE
  1855.     /*
  1856.      *  Since text to stdout is normally line buffered,
  1857.      *  if we do not write out a newline '\n' then the
  1858.      *  line is still buffered in memory and we have to
  1859.      *  fflush() to write it out.
  1860.      */
  1861.     #include <stdio.h>
  1862.     main()
  1863.     {
  1864.        char buf[256];
  1865.        printf("Enter a number -");
  1866.        fflush(stdout);
  1867.        fgets(buf, sizeof(buf), stdin);
  1868.        printf("Munch Munch...");
  1869.        fflush(stdout);
  1870.        sleep(1);
  1871.        puts("Thanks!");
  1872.     }
  1873.  
  1874. dice/fgetc,getc                                              dice/fgetc,getc
  1875.  
  1876.     FUNCTION
  1877.     fgetc: get a single character (ANSI)
  1878.     getc: get a single character from a file pointer (ANSI)
  1879.  
  1880.     SYNTAX
  1881.     #include <stdio.h>
  1882.     int c = fgetc(fp);
  1883.     int c = getc(fp); /* MACRO */
  1884.     FILE *fp;
  1885.  
  1886.     DESCRIPTION
  1887.     getc and fgetc both read a single character from a file pointer.  The
  1888.     value returned is actually an int because EOF (-1) must be
  1889.     differentiated from a 255.  Each returns an integer 0-255 or EOF (-1)
  1890.     if an end of file occurs.
  1891.  
  1892.     || NOTE: Refer to the file_pointer manual page for general
  1893.     || information.
  1894.  
  1895.     INPUTS
  1896.     FILE *fp;        file pointer
  1897.  
  1898.     RESULTS
  1899.     int c;            character 0 to 255, or EOF (-1).
  1900.  
  1901.     SEE ALSO
  1902.     putc, fputc, fread, fwrite
  1903.  
  1904.     EXAMPLE
  1905.     /*
  1906.      *  copy stdin to stdout using getc/putc.  Normally
  1907.      *  one uses fread/fwrite, but I'll save that for the
  1908.      *  fread manual page.
  1909.      *  Note that I output the initial message to stderr so
  1910.      *  it does not get stuck into stdout in case the user
  1911.      *  has redirected stdout.
  1912.      */
  1913.     #include <stdio.h>
  1914.     main()
  1915.     {
  1916.        int c;
  1917.        fputs("Type a couple of lines, then ^\\ (EOF)\n",
  1918.           stderr);
  1919.        while ((c = getc(stdin)) != EOF)
  1920.        {
  1921.            putc(c, stdout);
  1922.        }
  1923.        return(0);
  1924.     }
  1925.  
  1926. dice/fgetpos                                                    dice/fgetpos
  1927.  
  1928.     FUNCTION
  1929.     get current file position (ANSI)
  1930.  
  1931.     SYNTAX
  1932.     #include <stdio.h>
  1933.     int error = fgetpos(fp, &pos);
  1934.     fpos_t pos;
  1935.  
  1936.     DESCRIPTION
  1937.     fgetpos returns the current seek position and is roughly equivalent
  1938.     to ftell.  fgetpos is a new ANSI call to better support C compilers
  1939.     that use 16 bit integers.  DICE uses 32 bit integers so fgetpos is
  1940.     not so useful.    fgetpos takes a file pointer and the address of a
  1941.     fpos_t type (a long).  It fills the fpos_t variable with the current
  1942.     file position and returns 0 if all went well, non-zero if an error
  1943.     occured.
  1944.  
  1945.     || NOTE: Refer to the file_pointer manual page for general
  1946.     || information.
  1947.  
  1948.     INPUTS
  1949.     FILE *fp;        file pointer
  1950.  
  1951.     fpos_t *pos;        pointer to an fpos_t type that the position is
  1952.                 loaded into.
  1953.  
  1954.     RESULTS
  1955.     int error;        0 if no error, non-zero on error
  1956.  
  1957.     SEE ALSO
  1958.     ftell, rewind, fseek, rewind
  1959.  
  1960.     EXAMPLE
  1961.     /*
  1962.     **  Return the length of the file specified on the
  1963.     **  command line. */
  1964.     #include <stdio.h>
  1965.     main(int ac, char*av[])
  1966.     {
  1967.        FILE *fp;
  1968.        fpos_t off;
  1969.        if (ac == 1) {
  1970.           puts("Expected a filename argument");
  1971.           exit(1);
  1972.        }
  1973.        fp = fopen(av[1], "r");
  1974.        if (fp == NULL) {
  1975.           printf("Unable to open %s\n", av[1]);
  1976.           exit(5);
  1977.        }
  1978.        fseek(fp, 0L, SEEK_END);
  1979.        if (fgetpos(fp, &off)) {
  1980.           puts("Error getting file position");
  1981.           exit(20);
  1982.        }
  1983.        fclose(fp);
  1984.        printf("File %s is %d bytes long\n", av[1], off);
  1985.        return(0);
  1986.     }
  1987.  
  1988. dice/fgets                                                        dice/fgets
  1989.  
  1990.     FUNCTION
  1991.     get a line from a file pointer (ANSI)
  1992.  
  1993.     SYNTAX
  1994.     #include <stdio.h>
  1995.     char *ptr = fgets(buf, maxlen, fp);
  1996.     char *buf;
  1997.     int maxlen;
  1998.     FILE *fp;
  1999.  
  2000.     DESCRIPTION
  2001.     fgets gets a line from the specified file pointer, returning the
  2002.     first argument (buf) or NULL if an error or EOF occurs. fgets stores
  2003.     the line in buf, up to maxlen characters. This maximum includes a
  2004.     terminating newline '\n' and NULL '\0'. If more than maxlen-1
  2005.     characters are in the line fgets will terminate operation and put a
  2006.     NULL as the last character (so the buffer will still be a valid
  2007.     string). It is common to become confused by these functions:
  2008.  
  2009.  gets, puts
  2010.         These functions strip newline on input, add newline on output.
  2011.         gets works on stdin, puts on stdout.
  2012.  
  2013.  fgets, fputs
  2014.         These functions leave newlines alone on input, don't add to the
  2015.         output. Any file pointer, including stdin and stdout may be
  2016.         specified.
  2017.  
  2018.         || NOTE: Refer to the file_pointer manual page for general
  2019.         || information.
  2020.  
  2021.     INPUTS
  2022.     char *buf;        buffer
  2023.  
  2024.     int  maxlen;        maximum buffer size
  2025.  
  2026.     FILE *fp;        file pointer
  2027.  
  2028.     RESULTS
  2029.     char *ptr;        buf if all is well, or NULL if error or EOF
  2030.  
  2031.     SEE ALSO
  2032.     gets, puts, fputs, fread, getc, fgetc
  2033.  
  2034.     EXAMPLE
  2035.     #include <stdio.h>
  2036.     main()
  2037.     {
  2038.        unsigned char buf[128];
  2039.        short i;
  2040.        printf("Enter a line - ");
  2041.        fflush(stdout);
  2042.        if (fgets(buf, sizeof(buf), stdin) == NULL)
  2043.           exit(1);
  2044.        printf("In Hex: ");
  2045.        for (i = 0; buf[i]; ++i) printf(" %02x", buf[i]);
  2046.        puts("");
  2047.        return(0);
  2048.     }
  2049.  
  2050. dice/fhprintf                                                  dice/fhprintf
  2051.  
  2052.     FUNCTION
  2053.     formatted printing to a DOS file handle (AmigaDOS)
  2054.  
  2055.     SYNTAX
  2056.     #include <stdio.h>
  2057.     int n = fhprintf(fh, ctl, ...);
  2058.     BPTR fh;
  2059.     const char *ctl;
  2060.  
  2061.     DESCRIPTION
  2062.     fhprintf provides a method of using DICE's pfmt lib to do formatted
  2063.     printing to a file handle instead of a stdio file pointer. Output is
  2064.     unbuffered and thus not very efficient, but the call can be extremely
  2065.     useful when debugging libraries and such. If you have just a few
  2066.     things to print, and want to save the space the entire stdio package
  2067.     takes, you could use this function and _main.
  2068.  
  2069.     INPUTS
  2070.     BPTR fh;        DOS file handle
  2071.  
  2072.     const char *ctl;    format string, see printf()
  2073.  
  2074.     RESULTS
  2075.     int n;            number of characters output
  2076.  
  2077.     SEE ALSO
  2078.     printf, sprintf, vsprintf, fprintf, vfprintf, _main
  2079.  
  2080.     EXAMPLE
  2081.     void _main(int ac, char**av)
  2082.     {
  2083.        fhprintf(Output(), "The answer is %d!\n", 42);
  2084.     }
  2085.  
  2086. dice/file_descriptor                                    dice/file_descriptor
  2087.  
  2088.     FUNCTION
  2089.     file descriptor
  2090.  
  2091.     DESCRIPTION
  2092.     A file descriptor is the lowest portable access to the file system a
  2093.     C program may make.  File descriptors are used with open, read,
  2094.     write, close, etc.  A file descriptor is unbuffered (that is, every
  2095.     operation goes to the kernel and does not get buffered locally).
  2096.  
  2097.     || NOTE: Remember that a file descriptor is different from a stdio
  2098.     || file pointer (see the file_pointer manual page) and an AmigaDOS
  2099.     || file handle.
  2100.  
  2101. dice/fileno                                                      dice/fileno
  2102.  
  2103.     FUNCTION
  2104.     return file descriptor given a file pointer (UNIX)
  2105.  
  2106.     SYNTAX
  2107.     #include <stdio.h>
  2108.     int fd = fileno(fp); /* MACRO */
  2109.     FILE *fp;
  2110.  
  2111.     DESCRIPTION
  2112.     The fileno macro returns the file descriptor (open, close, read,
  2113.     write) associated with the file pointer (fopen, fclose, fread,
  2114.     fwrite). This is still not the AmigaDOS file handle; to get that you
  2115.     must use the fdtofh call.
  2116.  
  2117.     ## WARNING: If you use the file descriptor of a file pointer the file
  2118.     ## pointer will get its seek position confused. Additionally, there
  2119.     ## might be unflushed data in the file pointer's buffers that has not
  2120.     ## been written out to the file descriptor yet.  There also might be
  2121.     ## unread input on the file pointer's input buffers already read from
  2122.     ## the file descriptor.
  2123.  
  2124.     || NOTE: Refer to the file_pointer manual page for general
  2125.     || information.
  2126.  
  2127.     INPUTS
  2128.     FILE *fp;        file pointer
  2129.  
  2130.     RESULTS
  2131.     int  fd;        associated file descriptor
  2132.  
  2133.     SEE ALSO
  2134.     fdopen, fopen, fclose, open, close
  2135.  
  2136. dice/file_pointer                                          dice/file_pointer
  2137.  
  2138.     FUNCTION
  2139.     STDIO file_pointer (ANSI)
  2140.  
  2141.     DESCRIPTION
  2142.     A file pointer is the basis for stdio, a standard file management
  2143.     package available across all versions of C.  If you stick to the
  2144.     standard functions, your program should work on any machine that can
  2145.      compile C.
  2146.  
  2147.     The specific Amiga implementation builds file pointers (type "FILE
  2148.     *") on top of file descriptors (type "int"; see the file_descriptor
  2149.     manual page).  File pointers have a layer of buffering (good if you
  2150.     use many small reads and writes), while file descriptors pass
  2151.     requests directly to AmigaDOS.    Note that a stdio file pointer is not
  2152.     a file descriptor nor is a AmigaDOS file handle.  You may call only
  2153.     stdio routines (fopen, fclose, fread, fwrite, etc.) with file
  2154.     pointers.
  2155.  
  2156.     Some C implementations flush stdout whenever stdin is read.  DICE
  2157.     does not do this.
  2158.  
  2159. dice/fopen                                                        dice/fopen
  2160.  
  2161.     FUNCTION
  2162.     open a file and create a file pointer (ANSI)
  2163.  
  2164.     SYNTAX
  2165.     #include <stdio.h>
  2166.     FILE *fp = fopen(filename, modes)
  2167.     char *filename;
  2168.     char *modes;
  2169.  
  2170.     DESCRIPTION
  2171.     fopen is the grand master of stdio:  it opens and possibly creates a
  2172.     file and returns a new file pointer for use by the program. The first
  2173.     argument is the file to open, the second is a string containing one
  2174.     or mode characters defined as follows:
  2175.  
  2176.  Mode : Usage
  2177.  =====+======================================================================
  2178.  r    : open for reading, the file must already exist
  2179.  -----+----------------------------------------------------------------------
  2180.  w    : open for writing, the file is created if it does not exist,
  2181.       : truncated if it does
  2182.  -----+----------------------------------------------------------------------
  2183.  a    : open for append, writes always append to the file.
  2184.  -----+----------------------------------------------------------------------
  2185.  a    : the file starts out positioned at the end instead of at the
  2186.       : beginning.  This mode also creates the file but only if it does not
  2187.       : already exist.
  2188.  -----+----------------------------------------------------------------------
  2189.  r+   : also allows writing to the file in addition to reading
  2190.  -----+----------------------------------------------------------------------
  2191.  w+   : also allows reading from the file
  2192.  -----+----------------------------------------------------------------------
  2193.  b    : open for binary read/write, else the file is assumed to  contain
  2194.       : text (this is ignored by DICE since there is no difference  on the
  2195.       : Amiga).
  2196.  -----+----------------------------------------------------------------------
  2197.  
  2198.     All combinations except "rw" are allowed.  One uses "r+" or "w+"
  2199.     instead of "rw".  By the above description "r+" is used to update an
  2200.     existing file while "w+" is used to create a new file and then allow
  2201.     reads as well as writes to it. "wa" is equivalent to creating a new
  2202.     file and then appending to it. "r+a" is equivalent to appending to an
  2203.     already existing file. Other examples of valid modes combinations:
  2204.     "r+b", "w+b", "rb", "wb", "ab", "w", "r", "r+", "a", etc...
  2205.  
  2206.     || NOTE: Refer to the file_pointer manual page for general
  2207.     || information.
  2208.  
  2209.     INPUTS
  2210.     char *filename;     file name to open
  2211.  
  2212.     char *modes;        open modes string
  2213.  
  2214.     RESULTS
  2215.     FILE *fp;        new file pointer
  2216.  
  2217.     SEE ALSO
  2218.     fdopen, fclose, open, close
  2219.  
  2220.     EXAMPLE
  2221.     #include <stdio.h>
  2222.     main()
  2223.     {
  2224.     FILE    *fp;
  2225.     char *    filename = "t:XX";
  2226.     char *    why     = "I don't do windows\n";
  2227.  
  2228.         fp = fopen(filename, "wb");
  2229.         if( fp ) {
  2230.         fwrite(why, 1, strlen(why), fp);
  2231.         fwrite(why, strlen(why), 1, fp);
  2232.         fclose( fp );
  2233.         } else {
  2234.         printf("Can't open file %s\n", filename);
  2235.         }
  2236.         return(0);
  2237.  
  2238.     }
  2239.  
  2240. dice/fputc,putc                                              dice/fputc,putc
  2241.  
  2242.     FUNCTION
  2243.     fputc: write a single character (ANSI)
  2244.     putc: write a single character (ANSI)
  2245.  
  2246.     SYNTAX
  2247.     #include <stdio.h>
  2248.     int c = fputc(c, fp);
  2249.     int c = putc(c, fp); /* MACRO */
  2250.     FILE *fp;
  2251.  
  2252.     DESCRIPTION
  2253.     fputc writes a single character to a file pointer.  If all goes well
  2254.     the character is returned, else EOF is returned. fputc is a function
  2255.     call while putc is a macro
  2256.  
  2257.     || NOTE: Refer to the file_pointer manual page for general
  2258.     || information.
  2259.  
  2260.     INPUTS
  2261.     int c;            character to write
  2262.  
  2263.     FILE *fp;        file pointer
  2264.  
  2265.     RESULTS
  2266.     int c;            character written (same as first argument) or EOF
  2267.                 if error.
  2268.  
  2269.     SEE ALSO
  2270.     getc, putc, fputc, fread, fwrite, puts, fputs, gets, fgets
  2271.  
  2272.     EXAMPLE
  2273.     /*
  2274.      *  copy stdin to stdout using fgetc/fputc. Normally one
  2275.      *  uses fread/fwrite, but I'll save that for the fread
  2276.      *  manual page. note that I output the initial message
  2277.      *  to stderr so it does not get stuck into stdout in
  2278.      *  case the user has redirected stdout.
  2279.      */
  2280.     #include <stdio.h>
  2281.     main()
  2282.     {
  2283.        int c;
  2284.        fputs("Type a couple of lines, then ^\\ (EOF)\n",
  2285.           stderr);
  2286.        while ((c = fgetc(stdin)) != EOF)
  2287.        {
  2288.           fputc(c, stdout);
  2289.        }
  2290.        return(0);
  2291.     }
  2292.  
  2293. dice/fputs,puts                                              dice/fputs,puts
  2294.  
  2295.     FUNCTION
  2296.     fputs: write a string to a file pointer (ANSI)
  2297.     puts: then write a string to stdout, appending newline (ANSI)
  2298.  
  2299.     SYNTAX
  2300.     #include <stdio.h>
  2301.     int error = fputs(s, fp);
  2302.     int error = puts(s);
  2303.     const char *s;
  2304.     FILE *fp;
  2305.  
  2306.     DESCRIPTION
  2307.     fputs writes a string to a file pointer all the way up to, but not
  2308.     including, the NULL.  puts does the same thing but to stdout, and
  2309.     puts additionally writes a newline out.
  2310.  
  2311.     || NOTE: Refer to the file_pointer manual page for general
  2312.     || information.
  2313.  
  2314.     It is common to get confused between fputs and puts. Remember that
  2315.     puts adds a newline to the output while fputs does not.  gets strips
  2316.     the newline from an input line while fgets does not.
  2317.  
  2318.     INPUTS
  2319.     char *s;        string to write
  2320.  
  2321.     FILE *fp;        file pointer
  2322.  
  2323.     RESULTS
  2324.     int error;        0 or positive if all went ok, else negative. Note
  2325.                 that unlike printf() routines the numberr of
  2326.                 chars written out is NOT returned.
  2327.  
  2328.     SEE ALSO
  2329.     getc, putc, fputc, fread, fwrite, gets, fgets
  2330.  
  2331.     EXAMPLE
  2332.     #include <stdio.h>
  2333.     main()
  2334.     {
  2335.         /* note newline */
  2336.         fputs("This is a test of fputs\n", stdout);
  2337.         puts("This is a test of puts"); /*  note lack of */
  2338.         puts("That's it!");
  2339.         return(0);
  2340.     }
  2341.  
  2342. dice/fseek                                                        dice/fseek
  2343.  
  2344.     FUNCTION
  2345.     seek within a file pointer (ANSI)
  2346.  
  2347.     SYNTAX
  2348.     #include <stdio.h>
  2349.     int error = fseek(fp, offset, how);
  2350.     FILE *fp;
  2351.     long offset;
  2352.     int how;
  2353.  
  2354.     DESCRIPTION
  2355.     fseek changes the current seek position within a file. Offset is
  2356.     interpreted according to the how argument:
  2357.  
  2358.          :     :
  2359.     =========+===+=======================================================
  2360.     SEEK_SET : 0 : skip to position relative to beginning of file
  2361.     ---------+---+-------------------------------------------------------
  2362.     SEEK_CUR : 1 : skip to position relative to current position in file
  2363.     ---------+---+-------------------------------------------------------
  2364.     SEEK_END : 2 : skip to position relative to end of file.
  2365.     ---------+---+-------------------------------------------------------
  2366.  
  2367.     So, for example, one may seek to the beginning of a file by fseek(fp,
  2368.     0L, SEEK_SET);, to the end of the file by fseek(fp, 0L, SEEK_END);.
  2369.     Calling getc at this time would return an immediate EOF.  You can
  2370.     skip characters in a file with something like fseek(fp, 5L,
  2371.     SEEK_CUR); which skips 5 characters.  Note that when seeking relative
  2372.     to the end of the file, negative offsets are used.  For example, to
  2373.     seek to the very last character in the file you would use fseek(fp,
  2374.     -1L, SEEK_END); fseek returns 0 on success, a negative number on
  2375.     ERROR.    A common mistake is to expect fseek to return the new
  2376.     position  of the file but this is not what is returned.  Use ftell or
  2377.     fgetpos to determine the current offset into a file.
  2378.  
  2379.     || NOTE: Refer to the file_pointer manual page for general
  2380.     || information fseek flushes any buffered write data before seeking.
  2381.  
  2382.     INPUTS
  2383.     FILE *fp;        file pointer to seek
  2384.  
  2385.     long offset;        relative offset, depending on how
  2386.  
  2387.     int how;        0, 1, or 2 (absolute, relative, end- relative)
  2388.  
  2389.     RESULTS
  2390.     int error;        Error code from operation
  2391.  
  2392.     SEE ALSO
  2393.     ftell, fgetpos, fsetpos, rewind
  2394.  
  2395.     EXAMPLE
  2396.     see fopen for an example
  2397.  
  2398. dice/fread                                                        dice/fread
  2399.  
  2400.     FUNCTION
  2401.     read data from a file pointer (ANSI)
  2402.  
  2403.     SYNTAX
  2404.     #include <stdio.h>
  2405.     size_t robjs = fread(buf, objsize, nobjs, fp);
  2406.     void *buf; size_t objsize; size_t nobjs;
  2407.     FILE *fp;
  2408.  
  2409.     DESCRIPTION
  2410.     fread reads an arbitrary number of objects from a file pointer into
  2411.     the specified buffer and returns the actual number of objects read.
  2412.     If the return value robjs is not equal to nobjs then fread was unable
  2413.     to read the requested number of objects due to either a read error or
  2414.     an EOF condition.  If the file is already completely exhausted fread
  2415.     simply returns 0. Having two size arguments, an object size and a
  2416.     number of objects, simplifies the reading of structure arrays off
  2417.     disk.
  2418.  
  2419.     || NOTE: To use fread to read an arbitrary number of bytes one
  2420.     || normally uses the form: r = fread(buf, 1, n, fp); that is, n
  2421.     || objects of size 1.  fread will attempt to read objsize * nobjs
  2422.     || bytes into the specified buffer.
  2423.  
  2424.     INPUTS
  2425.     void   *buf;        buffer to load data into
  2426.  
  2427.     size_t objsize;     size of one object
  2428.  
  2429.     size_t nobjs;        number of objects to read
  2430.  
  2431.     FILE   *fp;        file pointer to read objects from
  2432.  
  2433.     RESULTS
  2434.     size_t robjs;        number of objects actually read or 0 if EOF or
  2435.                 ERROR.
  2436.  
  2437.     SEE ALSO
  2438.     fwrite, fopen, fclose, fseek, ftell, rewind
  2439.  
  2440. dice/free                                                          dice/free
  2441.  
  2442.     FUNCTION
  2443.     free memory allocated by calloc, malloc, or strdup (ANSI)
  2444.  
  2445.     SYNTAX
  2446.     #include <stdlib.h>
  2447.     void free(ptr);
  2448.     void *ptr;
  2449.  
  2450.     DESCRIPTION
  2451.     free frees memory allocated by calloc, malloc, or strdup.
  2452.  
  2453.     || NOTE: It is illegal to free(NULL). If free is given an illegal
  2454.     || pointer or NULL, it will freeze the process by calling wait(0L).
  2455.  
  2456.     INPUTS
  2457.     void *ptr;        pointer to memory to free
  2458.  
  2459.     SEE ALSO
  2460.     malloc, calloc, strdup
  2461.  
  2462.     EXAMPLE
  2463.     see calloc example
  2464.  
  2465. dice/freopen                                                    dice/freopen
  2466.  
  2467.     FUNCTION
  2468.     reopen a new file using an existing file pointer, the existing file
  2469.     is closed before it is reused (ANSI)
  2470.  
  2471.     SYNTAX
  2472.     #include <stdio.h>
  2473.     FILE *fp = freopen(filename, modes, ofp)
  2474.     char *filename;
  2475.     char *modes;
  2476.     FILE *ofp;
  2477.  
  2478.     DESCRIPTION
  2479.     freopen works exactly like fopen but takes an additional argument,  a
  2480.     file pointer to reuse. This file pointer, ofp, must reference a valid
  2481.     open file.  freopen will close out ofp then reuse the descriptor to
  2482.     open the new file, returning ofp (fp == ofp) on success, NULL on
  2483.     failure. If the freopen fails NULL is returned and the original file
  2484.     pointer is still closed, but not freed so you may call freopen again
  2485.     with the same ofp, even though it has already been closed.  Refer to
  2486.     the fopen manual page for information on the modes string. freopen is
  2487.     often used to change a program's stdin, stdout, or stderr though to
  2488.     be frank, using a separate file pointer is normally much more
  2489.     modular.
  2490.  
  2491.     ## WARNING: ANSI does not specify that the ofp can be used in a
  2492.     ## second freopen if the first freopen using ofp fails (returns
  2493.     ## NULL). Many implementations free the file pointer. This just might
  2494.     ## be the proper way of doing things but we don't know.  We suggest
  2495.     ## you do not use DICE's feature in that respect as we might have to
  2496.     ## change it back to free ofp if the new file is unopenable.
  2497.  
  2498.     || NOTE: Refer to the file_pointer manual page for general
  2499.     || information
  2500.  
  2501.     INPUTS
  2502.     char *filename;     file name to open
  2503.  
  2504.     char*modes;        open modes string
  2505.  
  2506.     FILE *ofp;        open file pointer to reuse
  2507.  
  2508.     RESULTS
  2509.     FILE *fp;        same as ofp if the new open worked, NULL
  2510.                 otherwise
  2511.  
  2512.     SEE ALSO
  2513.     fdopen, fopen, fclose, open, close
  2514.  
  2515.     EXAMPLE
  2516.     /*
  2517.      *  re-open stdin to an Amiga console device
  2518.      */
  2519.     #include <stdio.h>
  2520.     #include <assert.h>
  2521.     main()
  2522.     {
  2523.        char buf[256];
  2524.        assert(freopen("CON:0/0/320/100/freopen-in", "r",
  2525.           stdin));
  2526.        assert(freopen("CON:320/0/320/100/freopen-out", "w",
  2527.           stdout));
  2528.        /*
  2529.         *  set to line buffered
  2530.         */
  2531.        setvbuf(stdin, NULL, _IOLBF, 0);
  2532.        setvbuf(stdout, NULL, _IOLBF, 0);
  2533.        puts("Type a (short) line in the second window");
  2534.        gets(buf);
  2535.        fclose(stdin);
  2536.        fclose(stdout);
  2537.        fprintf(stderr, "Your line was: %s\n", buf);
  2538.        return(0);
  2539.     }
  2540.  
  2541. dice/fsetpos                                                    dice/fsetpos
  2542.  
  2543.     FUNCTION
  2544.     set position within file pointer (nearly equivalent to fseek
  2545.     absolute) (ANSI)
  2546.  
  2547.     SYNTAX
  2548.     #include <stdio.h>
  2549.     int error = fsetpos(fp, &pos);
  2550.     FILE *fp; fpos_t pos;
  2551.  
  2552.     DESCRIPTION
  2553.     fsetpos is a nearly useless call that is essentially the same as
  2554.     fseek(fp, (long)pos, 0); fsetpos seeks within a file pointer to the
  2555.     absolute position specified by an fpos_t type.    The address of an
  2556.     fpos_t object is passed to fsetpos. Normally one saves the current
  2557.     seek position into an fpos_t type using the fgetpos function, then
  2558.     seeks back using the fsetpos function.    In this way the programmer
  2559.     need not make any direct reference to the contents of the fpos_t
  2560.     type.
  2561.  
  2562.     INPUTS
  2563.     FILE *fp;        file pointer to seek
  2564.  
  2565.     fpos_t *pos;        pointer to fpos_t type previously initialized by
  2566.                 a fgetpos() call.
  2567.  
  2568.     RESULTS
  2569.     int error;        0 if no error, < 0 if error
  2570.  
  2571.     SEE ALSO
  2572.     ftell, fsetpos, fseek, rewind
  2573.  
  2574.     EXAMPLE
  2575.     /*
  2576.      *  get a line, save current position, get rest of
  2577.      *  file, go back to saved position, retrieve line
  2578.      *  again and print again.
  2579.      */
  2580.     #include <stdio.h>
  2581.     main(int ac, char*av[])
  2582.     {
  2583.        FILE *fp;
  2584.        fpos_t save_pos;
  2585.        int count;
  2586.        char buf[256];
  2587.        if (ac == 1)
  2588.        {
  2589.           puts("Expected textfile argument");
  2590.           exit(1);
  2591.        }
  2592.        fp = fopen(av[1], "r");
  2593.        if (fp == NULL)
  2594.        {
  2595.           printf("Unable to open %s\n", av[1]);
  2596.           exit(1);
  2597.        }
  2598.        for    (count = 0; fgets(buf, sizeof(buf), fp);
  2599.               ++count)
  2600.        {
  2601.           if (count == 0) /*  just before second line */
  2602.          fgetpos(fp, &save_pos);
  2603.           fprintf(stdout, "%-3d: %s", count + 1, buf);
  2604.        }
  2605.        if (count < 2)
  2606.        {
  2607.           puts("not enough lines in file for example!");
  2608.           exit(1);
  2609.        }
  2610.        puts("--end of file, now seeking back to line 2--");
  2611.        fsetpos(fp, &save_pos);
  2612.        if (fgets(buf, sizeof(buf), fp) == NULL)
  2613.        {
  2614.           puts("error!");
  2615.           exit(1);
  2616.        }
  2617.        fprintf(stdout, "%-3d: %s", 2, buf);
  2618.        fclose(fp);
  2619.        return(0);
  2620.     }
  2621.  
  2622. dice/fstat                                                        dice/fstat
  2623.  
  2624.     FUNCTION
  2625.     stat a file descriptor (UNIX)
  2626.  
  2627.     SYNTAX
  2628.     #include <sys/stat.h>
  2629.     int error = fstat(fd, &stat_buf);
  2630.     struct stat stat_buf;
  2631.  
  2632.     DESCRIPTION
  2633.     fstat is a UNIX-compatible call that returns information pertaining
  2634.     to the file represented by an open file descriptor. See stat for
  2635.     information on the struct stat fields.
  2636.  
  2637.     || NOTE: fstat works just like stat except you provide a UNIX file
  2638.     ||  descriptor (not an AmigaDOS File Handle).
  2639.     || Under 2.0, ExamineFH is used.  Under 1.3, the original path used
  2640.     || to open the file will be stat'd, which ends up scanning the
  2641.     || directory if the file was open for exclusive access.
  2642.  
  2643.     INPUTS
  2644.     int  fd;        file descriptor to stat
  2645.  
  2646.     struct stat *sbuf;
  2647.                 address of stat structure that will be filled in
  2648.  
  2649.     RESULTS
  2650.     int error;        0 on success, < 0 on error
  2651.  
  2652.     SEE ALSO
  2653.     chdir
  2654.  
  2655.     EXAMPLE
  2656.     #include <stdio.h>
  2657.     #include <fcntl.h>
  2658.     #include <sys/stat.h>
  2659.     main(int ac, char**av)
  2660.     {
  2661.        int r, fd;
  2662.        struct stat stat_buf;
  2663.        if (ac == 1) {
  2664.           puts("Expected a file name");
  2665.           exit(1);
  2666.        }
  2667.         fd = open(av[1], O_RDONLY );
  2668.        if( fd ) {
  2669.           r = fstat(fd, &stat_buf);
  2670.           if (r < 0)
  2671.         printf("Can't stat fd=%d\n", fd);
  2672.           else {
  2673.         printf("File is %d bytes\n", stat_buf.st_size);
  2674.         printf("modified %s", ctime(&stat_buf.st_ctime));
  2675.           }
  2676.           close(fd);
  2677.        }
  2678.        return(0);
  2679.     }
  2680.  
  2681. dice/ftell                                                        dice/ftell
  2682.  
  2683.     FUNCTION
  2684.     return current position within file pointer (ANSI)
  2685.  
  2686.     SYNTAX
  2687.     #include <stdio.h>
  2688.     long pos = ftell(fp);
  2689.     FILE *fp;
  2690.  
  2691.     DESCRIPTION
  2692.     ftell returns the current absolute seek offset within a file pointer.
  2693.  
  2694.     INPUTS
  2695.     FILE *fp;        file pointer retrieve seek position from
  2696.  
  2697.     RESULTS
  2698.     long pos;        current absolute seek position in file
  2699.  
  2700.     SEE ALSO
  2701.     ftell, fgetpos, fsetpos, fseek, rewind
  2702.  
  2703.     EXAMPLE
  2704.     /*
  2705.      * get a line, save current position, get rest of file,
  2706.      * go backto saved position, retrieve line again and
  2707.      * print again.
  2708.      * like fsetpos() example but uses ftell()/fseek()
  2709.      * instead
  2710.      */
  2711.     #include <stdio.h>
  2712.     main(int ac, char**av)
  2713.     {
  2714.        FILE *fp;
  2715.        long save_pos;
  2716.        int count;
  2717.        char buf[256];
  2718.        if (ac == 1)
  2719.        {
  2720.           puts("Expected textfile argument");
  2721.           exit(1);
  2722.        }
  2723.        fp = fopen(av[1], "r");
  2724.        if (fp == NULL)
  2725.        {
  2726.           printf("Unable to open %s\n", av[1]);
  2727.           exit(1);
  2728.        }
  2729.        for (count = 0; fgets(buf, sizeof(buf), fp); ++count)
  2730.        {
  2731.           if (count == 0)      /*  just before second line */
  2732.          save_pos = ftell(fp);
  2733.           fprintf(stdout, "%-3d: %s", count + 1, buf);
  2734.        }
  2735.        if (count < 2)
  2736.        {
  2737.           puts("not enough lines in file for example!");
  2738.           exit(1);
  2739.        }
  2740.        puts("--end of file, now seeking back to line 2--");
  2741.        fseek(fp, save_pos, SEEK_SET);
  2742.        if (fgets(buf, sizeof(buf), fp) == NULL)
  2743.        {
  2744.           puts("error!");
  2745.           exit(1);
  2746.        }
  2747.        fprintf(stdout, "%-3d: %s", 2, buf);
  2748.        fclose(fp);
  2749.        return(0);
  2750.     }
  2751.  
  2752. dice/fwrite                                                      dice/fwrite
  2753.  
  2754.     FUNCTION
  2755.     write data to a file pointer (ANSI)
  2756.  
  2757.     SYNTAX
  2758.     #include <stdio.h>
  2759.     size_t robjs = fwrite(buf, objsize, nobjs, fp);
  2760.     const void *buf;
  2761.     size_t objsize;
  2762.     size_t nobjs;
  2763.     FILE *fp;
  2764.  
  2765.     DESCRIPTION
  2766.     fwrite writes the specified number of objects to a file pointer from
  2767.     the specified buffer and returns the actual number of objects written
  2768.     or 0 or -1 depending on the error.  If the return value robjs is not
  2769.     equal to nobjs then a write error occured. Having two size arguments,
  2770.     an object size and number of objects, simplifies the reading of
  2771.     structure arrays off disk.
  2772.  
  2773.     || NOTE: To use fwrite to read an arbitrary number of bytes one
  2774.     || normally uses the form "r = fwrite(buf, 1, n, fp);", that is, n
  2775.     || objects of size 1.
  2776.  
  2777.     INPUTS
  2778.     void *buf;        buffer to copy data from
  2779.  
  2780.     size_t objsize;     size of one object
  2781.  
  2782.     size_t nobjs;        number of objects to write
  2783.  
  2784.     FILE *fp;        file pointer to read objects from
  2785.  
  2786.     RESULTS
  2787.     size_t robjs;        number of objects actually written (0 or EOF on
  2788.                 error)
  2789.  
  2790.     SEE ALSO
  2791.     fread, fopen, fclose, fseek, ftell, rewind
  2792.  
  2793. dice/getchar                                                    dice/getchar
  2794.  
  2795.     FUNCTION
  2796.     get character from stdin (ANSI)
  2797.  
  2798.     SYNTAX
  2799.     #include <stdio.h>
  2800.     int c = getchar(); /* MACRO */
  2801.  
  2802.     DESCRIPTION
  2803.     getchar returns the next available character on stdin or EOF if no
  2804.     more characters are available. getchar is equivalent to getc(stdin).
  2805.  
  2806.     || NOTE: Refer to the file_pointer manual page for general
  2807.     || information.
  2808.  
  2809.     INPUTS
  2810.     none
  2811.  
  2812.     RESULTS
  2813.     int c;            character 0 to 255, or EOF (-1) returned from
  2814.                 stdin
  2815.  
  2816.     SEE ALSO
  2817.     putc, putchar, fputc, fread, fwrite, getc
  2818.  
  2819.     EXAMPLE
  2820.     /*
  2821.      *  copy stdin to stdout using getchar/putchar. Normally
  2822.      *  one uses fread/fwrite, but I'll save that for the
  2823.      *  fread manual page.
  2824.      *  Note that I output the initial message to stderr
  2825.      *  so it does not get stuck into stdout in case the
  2826.      *  user has redirected stdout.
  2827.      *
  2828.      *  See getc manual page for equivalent example using
  2829.      *  getc/putc
  2830.      */
  2831.     #include <stdio.h>
  2832.     main()
  2833.     {
  2834.        int c;
  2835.        fputs("Type a couple of lines, then ^\\ (EOF)\n",
  2836.           stderr);
  2837.        while ((c = getchar()) != EOF)
  2838.        {
  2839.            putchar(c);
  2840.        }
  2841.        return(0);
  2842.     }
  2843.  
  2844. dice/getcwd                                                      dice/getcwd
  2845.  
  2846.     FUNCTION
  2847.     get current working directory (UNIX)
  2848.  
  2849.     SYNTAX
  2850.     #include <stdio.h>
  2851.     char *path = getcwd(buf, max);
  2852.     non-standard call
  2853.  
  2854.     DESCRIPTION
  2855.     getcwd gets the current working directory and puts it into the
  2856.     specified buffer buf. If buf is NULL it will be malloc'd
  2857.     automatically. The parameter buf is returned (or the malloc'd buffer
  2858.     if you passed NULL for buf). The parameter max specifies the maximum
  2859.     length of the path including the terminating NULL character. NULL is
  2860.     returned if any error occurs (such as malloc failing)
  2861.  
  2862.     INPUTS
  2863.     char *buf;        buffer to place current directory path into or
  2864.                 NULL if you want getcwd to allocate one
  2865.  
  2866.     int max;        maximum size of buffer
  2867.  
  2868.     RESULTS
  2869.     char *path;        returns allocated buffer if you passed NULL for
  2870.                 buf, else returns the first argument.  Returns
  2871.                 NULL on error.
  2872.  
  2873.     SEE ALSO
  2874.     chdir
  2875.  
  2876.     EXAMPLE
  2877.     #include <stdio.h>
  2878.     char buf[512];
  2879.     main(int ac, char**av)
  2880.     {
  2881.        getcwd(buf, sizeof(buf));
  2882.        printf("Current directory is: %s\n", buf);
  2883.        return(0);
  2884.     }
  2885.  
  2886. dice/getenv                                                      dice/getenv
  2887.  
  2888.     FUNCTION
  2889.     get environment variable (ANSI)
  2890.  
  2891.     SYNTAX
  2892.     #include <stdlib.h>
  2893.     char *var = getenv(const char *name);
  2894.  
  2895.     DESCRIPTION
  2896.     getenv searches for and returns the ENV: enviroment variable
  2897.     requested.  getenv will cache variables so that requesting the same
  2898.     variable repetitously does not allocate a new memory buffer.  getenv
  2899.     allocates a buffer for each variable returned, so you do not have to
  2900.     copy the return value from getenv.  This memory is free'd on program
  2901.     exit.  Do not attempt to free a getenv'd variable!!
  2902.  
  2903.     INPUTS
  2904.     char *name;        Name of enviroment variable, on the Amiga this is
  2905.                 not case sensitive. On UNIX systems it is.
  2906.  
  2907.     RESULTS
  2908.     char *var;        Contents of enviroment variable or NULL if the
  2909.                 variable could not be found.
  2910.  
  2911.     EXAMPLE
  2912.     #include <stdio.h>
  2913.     #include <stdlib.h>
  2914.     main(int ac, char**av)
  2915.     {
  2916.        char *dccopts = getenv("DCCOPTS");
  2917.        if (dccopts)
  2918.           printf("DCCOPTS = %s\n", dccopts);
  2919.        else
  2920.           printf("You do not have a DCCOPTS enviroment\
  2921.               variable!\n");
  2922.        return(0);
  2923.     }
  2924.  
  2925. dice/getfnl                                                      dice/getfnl
  2926.  
  2927.     FUNCTION
  2928.     get file name list: scan directory, return list of files that match
  2929.     the optional wildcard (DICE)
  2930.  
  2931.     SYNTAX
  2932.     #include <stdlib.h>
  2933.     int n = getfnl(pat, buf, bufsize, attr);
  2934.  
  2935.     const char *pat;
  2936.     char *buf;
  2937.     int bufsize;
  2938.     int attr;
  2939.  
  2940.     DESCRIPTION
  2941.     getfnl scans the specified anchored AmigaDOS pattern and fills the
  2942.     specified buffer (up to bufsize bytes) with file names separated by a
  2943.     NULL character (\0), ending the list with a double NULL (\0\0).
  2944.     getfnl returns the number of files/dirs in the buffer or -1 if there
  2945.     is not enough room  The pattern pat is an AmigaDOS pattern such as
  2946.     "df0:#?.c". The parameter buf is a buffer of bufsize bytes.  The
  2947.     parameter attr determines what kinds of files are valid:    0 for
  2948.     normal files only, 1 for files and directories
  2949.  
  2950.     || NOTE: getfnl exists for compatibility only;    expand_args is a much
  2951.     || better function function to use if you want a list of files &
  2952.     || dirs.
  2953.  
  2954.     INPUTS
  2955.     const char *pat;    pattern to scan for (anchored)
  2956.  
  2957.     char *buf;        buffer to put results in
  2958.  
  2959.     int bufsize;        size of buffer
  2960.  
  2961.     int attr;        attribes (0 or 1)
  2962.  
  2963.     RESULTS
  2964.     int n;            number of file names in buffer or -1 on error
  2965.  
  2966.     SEE ALSO
  2967.     strbpl, expand_args
  2968.  
  2969.     EXAMPLE
  2970.     #include <stdio.h>
  2971.     char Buf[4096];
  2972.     main(int ac, char**av)
  2973.     {
  2974.        int n;
  2975.        if (ac != 2)
  2976.        {
  2977.           puts("Expected an anchored wildcard such as '#?'");
  2978.           exit(1);
  2979.        }
  2980.        n = getfnl(av[1], Buf, sizeof(Buf), 1);
  2981.        {
  2982.           char *ptr = Buf;
  2983.           while (*ptr)
  2984.           {
  2985.           /*  look for \0\0   */
  2986.           puts(ptr);
  2987.           ptr += strlen(ptr) + 1;
  2988.           /*  skip first \0   */
  2989.           }
  2990.        }
  2991.        return(0);
  2992.     }
  2993.  
  2994. dice/GetHead,GetTail,GetSucc,GetPred    dice/GetHead,GetTail,GetSucc,GetPred
  2995.  
  2996.     FUNCTION
  2997.     Manipulate EXEC style lists (DICE)
  2998.  
  2999.     SYNTAX
  3000.     #include <lists.h>
  3001.     struct Node *node = GetHead(list);
  3002.     struct Node *node = GetTail(list);
  3003.     struct Node *node = GetSucc(oldNode);
  3004.     struct Node *node = GetPred(oldNode);
  3005.     const struct Node *oldNode;
  3006.     const struct List *list;
  3007.  
  3008.     DESCRIPTION
  3009.     These functions allow scanning of EXEC style lists (which are also
  3010.     useful for many programs having nothing to do with EXEC). GetHead
  3011.     returns the first node in a list or NULL if the list is empty;
  3012.     GetTail returns the last node in a list or NULL if the list is empty;
  3013.     GetSucc returns the next node in a list (given some intermediate
  3014.     node) or NULL when we reach the end of the list GetPred returns the
  3015.     previous node in a list before some intermediate node or NULL when we
  3016.     reach the beginning of the list
  3017.  
  3018.     || NOTE: These are DICE functions and do not exist outside of DICE,
  3019.     || though they could be easily written.
  3020.  
  3021.     INPUTS
  3022.     struct List *list;
  3023.                 list to get head or tail node from
  3024.  
  3025.     struct Node *oldNode;
  3026.                 node from which to get relative successor or
  3027.                 predecessor from
  3028.  
  3029.     RESULTS
  3030.     struct Node *node;
  3031.                 returned node or NULL
  3032.  
  3033.     EXAMPLE
  3034.     /*
  3035.      *  A simple symbol create/delete/list program.
  3036.      *  (Note: for a real symbol table you'd want
  3037.      *  to use hash tables)
  3038.      */
  3039.     #include <lists.h>  /* non-standard header file */
  3040.     #include <stdio.h>
  3041.     #include <stdlib.h>
  3042.     #include <string.h>
  3043.     typedef struct List List;
  3044.     typedef struct Node Node;
  3045.     List SymList;
  3046.     void AddSymbol(char *);
  3047.     void DelSymbol(char *);
  3048.     Node *FindSymbol(char *);
  3049.     main()
  3050.     {
  3051.        char buf[256];
  3052.        char symBuf[256];
  3053.        short notDone = 1;
  3054.        NewList(&SymList);
  3055.        puts("(return for help)");
  3056.        while (notDone)
  3057.        {
  3058.           printf("Enter Command: ");
  3059.           fflush(stdout);
  3060.           if ( fgets(buf, sizeof(buf), stdin) == NULL)
  3061.          break;
  3062.           switch(buf[0])
  3063.           {
  3064.          case 'a': if (sscanf(buf + 1,"%s",symBuf)== 1)
  3065.             AddSymbol(symBuf);
  3066.             break;
  3067.          case 'd': if (sscanf(buf + 1,"%s",symBuf)== 1)
  3068.             DelSymbol(symBuf);
  3069.             break;
  3070.          case 'l': { Node *node;
  3071.             for (node = GetHead(&SymList); node;
  3072.             node = GetSucc(node))
  3073.             puts(node->ln_Name);
  3074.           }
  3075.           break;
  3076.           case 'q': notDone = 0;
  3077.             break;
  3078.          default: puts("<return>      -help   ");
  3079.             puts("a name-add symbol");
  3080.             puts("d name-delete symbol");
  3081.             puts("l     -list symbols");
  3082.             puts("q     -quit");
  3083.             break;
  3084.        }
  3085.     }
  3086.     puts("bye!");
  3087.     return(0);
  3088.     }
  3089.  
  3090.     void AddSymbol(name)
  3091.     char *name;
  3092.     {
  3093.        Node *node;
  3094.        if (FindSymbol(name))
  3095.        {
  3096.           puts("already exists!");
  3097.           exit(1);
  3098.        }
  3099.        if (node = malloc(sizeof(Node)))
  3100.        {
  3101.           AddTail(&SymList, node);
  3102.           node->ln_Name = strdup(name);
  3103.           /* bad code, not checking */
  3104.           /* for error result!    */
  3105.        }
  3106.     }
  3107.  
  3108.     void DelSymbol(name)
  3109.     char *name;
  3110.     {
  3111.        Node *node;
  3112.        if (node = FindSymbol(name))
  3113.        {
  3114.           Remove(node);   /*  take out of list    */
  3115.           free(node->ln_Name);    /*  free name   */
  3116.           free(node);     /*  free node last      */
  3117.           puts("ok");
  3118.           }
  3119.        else
  3120.        {
  3121.           puts("Couldn't find it!");
  3122.        } }
  3123.  
  3124.     Node * FindSymbol(name)
  3125.     char *name;
  3126.     {
  3127.        Node *node;
  3128.        for (node = GetHead(&SymList); node;
  3129.          node = GetSucc(node))
  3130.        {
  3131.         if (strcmp(node->ln_Name, name) == 0)
  3132.         return(node);
  3133.        }
  3134.        return(NULL);
  3135.     }
  3136.  
  3137. dice/gets                                                          dice/gets
  3138.  
  3139.     FUNCTION
  3140.     get a line from stdin (ANSI)
  3141.  
  3142.     SYNTAX
  3143.     #include <stdio.h>
  3144.     char *ptr = gets(buf);
  3145.     char *buf;
  3146.  
  3147.     DESCRIPTION
  3148.     gets takes a line from stdin and places it into your buffer.  A null
  3149.     (\0) is placed in the buffer instead of the newline (\n).  While the
  3150.     function can be convenient, if your buffer is not large enough, you
  3151.     loose. See fgets for a more reasonable function.
  3152.  
  3153.  gets, puts
  3154.         These functions strip newline on input, add newline on output.
  3155.         gets works on stdin, puts on stdout.
  3156.  
  3157.  fgets, fputs
  3158.         These functions leave newlines alone on input, don't add to the
  3159.         output. Any file pointer, including stdin and stdout may be
  3160.         specified.
  3161.  
  3162.         || NOTE: Refer to the file_pointer manual page for general
  3163.         || information.
  3164.  
  3165.     INPUTS
  3166.     char *buf;        buffer, must be able to maximum possible line
  3167.  
  3168.     RESULTS
  3169.     char *ptr;        buf if all is well, or NULL if error or EOF
  3170.  
  3171.     SEE ALSO
  3172.     puts, fputs, fgets, fread, getc, fgetc
  3173.  
  3174.     EXAMPLE
  3175.     #include <stdio.h>
  3176.     main()
  3177.     {
  3178.        char buf[128];
  3179.        printf("Enter a line - ");
  3180.        fflush(stdout);
  3181.        if (gets(buf) == NULL) // Enter a long line to crash
  3182.           exit(1);
  3183.        printf("Your line was: %s\n", buf);
  3184.        return(0);
  3185.     }
  3186.  
  3187. dice/isalnum,isalpha,iscntrl,isdigit,isgraph,islower,isprint,ispunct,isspace,isupper,isxdigit
  3188.  
  3189.     FUNCTION
  3190.     Test a character for assorted attributes (ANSI)
  3191.  
  3192.     SYNTAX
  3193.     #include <ctype.h>
  3194.     int r = isxxxxx(c);
  3195.     int c;
  3196.  
  3197.     || NOTE: These are MACROS if you #include <ctype.h>, function calls
  3198.     || if you do not.
  3199.  
  3200.     DESCRIPTION
  3201.     These function return non-zero (true) if the test is true, and zero
  3202.     (false) if not.
  3203.  
  3204.     || NOTE: When a non-zero value is returned, this value might be
  3205.     || anything other than zero.  It is not necessarily a 1.  It is
  3206.     || guaranteed to fit in a short, however, and still remain
  3207.     ||  non-zero.
  3208.     ||
  3209.     || Characters in the -1 to 255 range are valid inputs. Characters
  3210.     || less than -1 or larger than 255 are illegal and the results will
  3211.     || be random.  If you are passing a CHAR, you must cast it to an
  3212.     || UNSIGNED CHAR first. EOF is a valid input, and always returns
  3213.     || false
  3214.  
  3215.    isalpha  Returns non-zero if the character is a letter in the alphabet
  3216.         (a-z, A-Z).
  3217.  
  3218.    isalnum  Returns non-zero if the character is alphanumeric (a-z, A-Z,
  3219.         0-9).
  3220.  
  3221.    iscntrl  Returns non-zero if the character is a control character (decimal
  3222.         0 to 31).
  3223.  
  3224.    isdigit  Returns non-zero if the character is a digit ('0' through '9').
  3225.  
  3226.    isgraph  Returns non-zero if the character is printable and not a space.
  3227.         isgraph is the same as isprint, but with the space character
  3228.         excluded from the printable set.
  3229.  
  3230.    islower  Returns non-zero if the character is a lower case letter 'a' -
  3231.         'z'.
  3232.  
  3233.     isprint
  3234.  
  3235.     Returns non-zero if the character is printable. isprint is the same
  3236.     as isgraph, but with the space character included in the printable
  3237.     set.
  3238.  
  3239.    ispunct  Returns non-zero if the character is any sort of punctuation,
  3240.         including puntuation in the extended ASCII range.  Basically, if
  3241.         it is not a control character, alpha or a digit, it is
  3242.         punctuation.
  3243.  
  3244.    isspace  Returns non-zero if the character is any sort of white space.
  3245.         Included are space, shifted space, line feed (lf), form feed
  3246.         (ff), carriage return (cr), tab, and vertical tab.
  3247.  
  3248.    isupper  Returns non-zero if the character is an upper case letter 'A'
  3249.         -'Z'.
  3250.  
  3251.   isxdigit  Returns non-zero if the character is a valid hexadecimal digit
  3252.         '0' - '9', 'a' - 'f', or 'A' - 'F'.
  3253.  
  3254.         These macros use a lookup table.  One table handles the eight
  3255.         most common functions.  A second table handles ispunct, isxdigit
  3256.         and isprint.  It saves space if you reference only one table.
  3257.         There is space in the second table if you wish to add your own
  3258.         tests (you'll have to recompile the library source code).
  3259.  
  3260.     INPUTS
  3261.     int c;            character that we are checking
  3262.  
  3263.     RESULTS
  3264.     int r;            0 if the check failed, non-zero if the check is
  3265.                 true
  3266.  
  3267.     SEE ALSO
  3268.     tolower, toupper
  3269.  
  3270.     #include <stdio.h>
  3271.     #include <ctype.h>
  3272.     main()
  3273.     {
  3274.     int c;
  3275.  
  3276.         for( c=0; c<256; c++) {
  3277.         if( (c & 31) == 0 )    /* 32 per line */
  3278.             printf("\n%02x: ", c);
  3279.         if( isprint( c ) )
  3280.             printf("%c", c );   /* character */
  3281.         else
  3282.             printf("%c", 127 ); /* placeholder */
  3283.         }
  3284.         printf("\n");
  3285.         for( c=0; c<256; c++) {
  3286.         if( (c & 31) == 0 )    /* 32 per line */
  3287.             printf("\n%02x: ", c);
  3288.         if( ispunct( c ) )
  3289.             printf("x");        /* true */
  3290.         else
  3291.             printf("-");        /* false */
  3292.         }
  3293.         printf("\n");
  3294.         return( 0 );
  3295.     }
  3296.  
  3297. dice/isatty                                                      dice/isatty
  3298.  
  3299.     FUNCTION
  3300.     Test if a a file descriptor is a TTY (UNIX)
  3301.  
  3302.     SYNTAX
  3303.     #include <ctype.h>
  3304.     int r = isatty(fd); int fd;
  3305.  
  3306.     DESCRIPTION
  3307.     isatty returns TRUE (1) if the file descriptor is associated with a
  3308.     console, FALSE (0) if not, or -1 if an error condition occurs (such
  3309.     as illegal file descriptor).
  3310.  
  3311.     || NOTE: The standard input (0), standard output (1), and standard
  3312.     || error (2) can all return different values for isatty depending on
  3313.     || how the program is redirected.  A program whose standard in and
  3314.     || standard out is redirected may still have a standard error that is
  3315.     || connected to the console. Refer to the file_descriptor manual page
  3316.     || for general information.  Unlike file pointers and file handles,
  3317.     || the file descriptor is checked for validity and will simply return
  3318.     || an error if illegal.
  3319.  
  3320.     INPUTS
  3321.     int fd;         file descriptor
  3322.  
  3323.     RESULTS
  3324.     int r;            result, 1 if a tty, 0 if not, or -1 if error
  3325.  
  3326.     SEE ALSO
  3327.     close, creat, fcntl, fdtofh, getfh, ioctl, lseek, mkdir, open, read,
  3328.     rmdir, unlink, write
  3329.  
  3330. dice/ioctl                                                        dice/ioctl
  3331.  
  3332.     FUNCTION
  3333.     IO control on file descriptor (UNIX)
  3334.  
  3335.     SYNTAX
  3336.     #include <fcntl.h>
  3337.     int r = ioctl(fd, req, parg1, parg2);
  3338.     int fd;
  3339.     int req;
  3340.     int *parg1;
  3341.     int *parg2;
  3342.  
  3343.     DESCRIPTION
  3344.     ioctl executes an IO control on the file descriptor.  Currently no IO
  3345.     controls are implemented.
  3346.  
  3347.     INPUTS
  3348.     int fd;         file descriptor
  3349.  
  3350.     int req;        request from <ioctl.h>
  3351.  
  3352.     int *parg1;        address of argument #1
  3353.  
  3354.     int *parg2;        address of argument #2
  3355.  
  3356.     RESULTS
  3357.     int r;            result, error if < 0.
  3358.  
  3359.     SEE ALSO
  3360.     close, creat, fcntl, fdtofh, getfh, isatty, lseek, mkdir, open, read,
  3361.     rmdir, unlink, write
  3362.  
  3363. dice/localtime                                                dice/localtime
  3364.  
  3365.     FUNCTION
  3366.     convert time into broken down time (ANSI)
  3367.  
  3368.     SYNTAX
  3369.     #include <time.h>
  3370.     struct tm *tp = localtime(&t);
  3371.     time_t t;
  3372.  
  3373.     DESCRIPTION
  3374.     localtime takes the address of a time_t variable and breaks up the
  3375.     time into component parts, storing them in a static tm structure.
  3376.     The address of this structure is returned.  Since the broken up time
  3377.     is stored into a static structure, the structure will get overwritten
  3378.     on the next call to localtime. The fields of the tm structure are:
  3379.  
  3380.     struct tm {
  3381.            int tm_sec;     /*  0-59    */
  3382.            int tm_min;     /*  0-59    */
  3383.            int tm_hour;    /*  0-23    */
  3384.            int tm_mday;    /*  1-31    */
  3385.            int tm_mon;     /*  0-11    */
  3386.            int tm_year;    /*  n+1900  */
  3387.            int tm_wday;    /*  (sun)0-6*/
  3388.            int tm_yday;    /*  0-366   */
  3389.            int tm_isdst;   /*  is daylight savings? */
  3390.     /* (the is daylight flag is not implemented by DICE) */
  3391.     };
  3392.  
  3393.     INPUTS
  3394.     time_t *t;        pointer to a time_t
  3395.  
  3396.     RESULTS
  3397.     struct tm *tp;        pointer to a struct tm structure filled out
  3398.                 according to the passed time.
  3399.  
  3400.     SEE ALSO
  3401.     time, asctime, strftime, ctime, clock
  3402.  
  3403.     EXAMPLE
  3404.     /*
  3405.      *  Note that it is much easier to format time/date
  3406.      *  strings with strftime().
  3407.      */
  3408.     #include <stdio.h>
  3409.     #include <time.h>
  3410.     main()
  3411.     {
  3412.        time_t t = time(NULL);
  3413.        struct tm *tp = localtime(&t);
  3414.        printf("The time is %02d:%02d:%02d\n", tp->tm_hour,
  3415.             tp->tm_min, tp->tm_sec);
  3416.        return(0);
  3417.     }
  3418.  
  3419. dice/LockAddr,LockAddrB,TryLockAddr,TryLockAddrB,UnlockAddr,UnlockAddrB
  3420.  
  3421.     FUNCTION
  3422.     LockAddr: Gain Exclusive, Fast semaphore (bit 0)
  3423.     LockAddrB: Gain Exclusive, Fast semaphore (bit n 0-7)
  3424.     TryLockAddr: Non- Blocking version of LockAddr
  3425.     TryLockAddrB: Non-Blocking version of LockAddrB
  3426.     UnlockAddr: Release exclusive semaphore, bit 0
  3427.     UnlockAddrB: Release exclusive semaphore, bit n 0-7 (DICE)
  3428.  
  3429.     SYNTAX
  3430.     void LockAddr(lck);
  3431.     void LockAddrB(bitno, lck);
  3432.     int r = TryLockAddr(lck);
  3433.     int r = TryLockAddrB(bitno, lck);
  3434.     void UnlockAddr(lck);
  3435.     void UnlockAddrB(bitno, lck);
  3436.     long lck[2];
  3437.  
  3438.     DESCRIPTION
  3439.     These are custom DICE functions used for inter-task locking
  3440.     semaphores in programs that need such functions.  These routines are
  3441.     somewhat faster than standard Amiga semaphore routines and take less
  3442.     memory, though at the cost of DICE specific. To use an inter-task
  3443.     lock one first initializes an lck array to 0's. The entry long
  3444.     lck[2]; is an array of two longwords that the lock routines will use
  3445.     to do their stuff.  This array should be zero'd only once at program
  3446.     initialization time (the master task before any other tasks are
  3447.     created that use it). Each lck array may hold up to 8 locks, hence
  3448.     the LockAddrB calls. The non-B calls use lock #0 for simplicity.  For
  3449.      simplicity we will only discuss non-B calls.
  3450.     To gain a lock you may call LockAddr with the address of the lck
  3451.     array (which, being an array, does not need the & in the call). This
  3452.     routine will not return until the lock can be obtained. You may also
  3453.     use TryLockAddr to attempt to gain a lock. The return value is:    -1
  3454.     if the command is unable to obtain the lock (that is, it is in use),
  3455.     1 if the lock has been obtained.  To release an obtained lock you
  3456.     call UnlockAddr(lck).
  3457.  
  3458.     ## WARNING: DO NOT RELEASE A LOCK YOU DO NOT HAVE!
  3459.  
  3460.     INPUTS
  3461.     long *lck;        a pointer to two longwords, initially zero'd
  3462.  
  3463.     -int bitno;        lock # ... up to 8 independant locks exist for
  3464.                 each lck structure
  3465.  
  3466.     RESULTS
  3467.     int r;            (TryLock only), -1 on failure, 1 on success.
  3468.  
  3469.     EXAMPLE
  3470.     /*
  3471.      *  This program obtains a lock based at a public
  3472.      *  message port and holds it for ten seconds before
  3473.      *  releasing it. The public message port is left
  3474.      *  in memory (but only exists once no matter how
  3475.      *  many programs you run).
  3476.      *
  3477.      *  To test locking, open up two or more CLI's and run
  3478.      *  the program simultaniously (or as close as your
  3479.      *  fingers can make it) two or more times.  Only one
  3480.      *  program will 'have' the lock at a time.
  3481.      *
  3482.      *  we use AllocMem() so the port survives the program
  3483.      */
  3484.  
  3485.     #include <exec/types.h>
  3486.     #include <exec/ports.h>
  3487.     #include <exec/memory.h>
  3488.     #include <stdio.h>
  3489.     #include <stdlib.h>
  3490.     #include <assert.h>
  3491.  
  3492.     typedef struct
  3493.     {
  3494.        struct MsgPort Port;
  3495.        long   Lock[2];
  3496.     }
  3497.     MyPort;
  3498.  
  3499.     extern void *FindPort();
  3500.     extern void *CreatePort();
  3501.     extern void *AllocMem();
  3502.     MyPort *Port;
  3503.     short HaveLock;
  3504.  
  3505.     int brk()
  3506.     {
  3507.        if (HaveLock)
  3508.        UnlockAddr(Port->Lock);
  3509.        return(1);       /*  abort   */
  3510.     }
  3511.  
  3512.     main()
  3513.     {
  3514.        char *portName = "Lock-Test";
  3515.        onbreak(brk);
  3516.        Forbid();
  3517.        if ((Port = FindPort(portName)) == NULL)
  3518.        {
  3519.           MyPort *port;
  3520.           port = AllocMem(sizeof(MyPort) + strlen(portName)
  3521.                   + 1, MEMF_PUBLIC | MEMF_CLEAR);
  3522.                   assert(port);
  3523.           port->Port.mp_Node.ln_Name = (char *)(port + 1);
  3524.           port->Port.mp_Node.ln_Type = NT_MSGPORT;
  3525.           strcpy(port->Port.mp_Node.ln_Name, portName);
  3526.           AddPort(port);
  3527.           Port = port;
  3528.        }
  3529.        Permit();
  3530.        puts("getting lock");
  3531.        LockAddr(Port->Lock);
  3532.        HaveLock = 1;
  3533.        puts("Got the lock!, sleeping for 10 seconds");
  3534.        sleep(10);
  3535.        UnlockAddr(Port->Lock);
  3536.        HaveLock = 0;
  3537.        puts("released lock");
  3538.        return(0);
  3539.     }
  3540.  
  3541. dice/log,flog                                                  dice/log,flog
  3542.  
  3543.     FUNCTION
  3544.     log: return the log of a double, base e (ANSI)
  3545.     flog: return the log a float, base e (ANSI)
  3546.  
  3547.     LIBRARY
  3548.     m.lib
  3549.  
  3550.     SYNTAX
  3551.     #include <math.h>
  3552.     double a = log(b);
  3553.     double b;
  3554.     float  c = flog(d);
  3555.     float  d;
  3556.  
  3557.     DESCRIPTION
  3558.     The former command returns the log of the double quantity, base e;
  3559.     the latter returns the log of the float quantity, base e.
  3560.  
  3561.     INPUTS
  3562.     double b;        double floating point value float d; float
  3563.                 floating point value
  3564.  
  3565.     RESULTS
  3566.     double a;        result double floating point value float c;
  3567.                 result float  floating point value
  3568.  
  3569.     SEE ALSO
  3570.     acos, asin, atan, cos, exp, fabs, log10, pow, sin, sqrt, tan facos,
  3571.     fasin
  3572.  
  3573. dice/log10,flog10                                          dice/log10,flog10
  3574.  
  3575.     FUNCTION
  3576.     log10: return the logarithm of a double, base 10 (ANSI)
  3577.     flog10: return the logarithm of a float, base 10 (ANSI)
  3578.  
  3579.     LIBRARY
  3580.     m.lib
  3581.  
  3582.     SYNTAX
  3583.     #include <math.h>
  3584.     double a = log10(b);
  3585.     double b;
  3586.     float  c = flog10(d);
  3587.     float  d;
  3588.  
  3589.     DESCRIPTION
  3590.     log10 returns the log of a double quantity, base 10; flog10 returns
  3591.     the log of the floating point quantity, base 10.
  3592.  
  3593.     INPUTS
  3594.     double b;        double floating point value
  3595.  
  3596.     float d;        float floating point value
  3597.  
  3598.     RESULTS
  3599.     double a;        result double floating point value
  3600.  
  3601.     float c;        result float  floating point value
  3602.  
  3603.     SEE ALSO
  3604.     acos, asin, atan, cos, exp, fabs, log, pow, sin, sqrt, tan facos,
  3605.     fasin
  3606.  
  3607.     EXAMPLE
  3608.     /*
  3609.      *  compile with the math library -lm
  3610.      */
  3611.     #include <math.h>
  3612.     #include <stdio.h>
  3613.     main()
  3614.     {
  3615.        {
  3616.           double a = log10(0.25);
  3617.           printf("log10 0.25 = %lf\n", a); /* -0.6021 */
  3618.        }
  3619.        {  /*  less accuracy   */
  3620.           float a = flog10(0.25);
  3621.           printf("log10 0.25 = %lf\n", (double)a);
  3622.        }
  3623.        return(0);
  3624.     }
  3625.  
  3626. dice/lseek                                                        dice/lseek
  3627.  
  3628.     FUNCTION
  3629.     seek within a file descriptor (UNIX)
  3630.  
  3631.     SYNTAX
  3632.     #include <fcntl.h>
  3633.     long newpos = lseek(fd, offset, how)
  3634.     int fd;
  3635.     long offset;
  3636.     int how;
  3637.  
  3638.     DESCRIPTION
  3639.     lseek changes where the file descriptor points to within the open
  3640.     file.  You may specify an offset relative to the  beginning of the
  3641.     file, the current position in the file, or the end of the file:
  3642.  
  3643.     Val : Purpose
  3644.     ====+=========================================================
  3645.     0   : absolute offset (relative to the beginning of the file)
  3646.     ----+---------------------------------------------------------
  3647.     1   : offset relative to the current position in the file
  3648.     ----+---------------------------------------------------------
  3649.     2   : offset relative to the end of the file
  3650.     ----+---------------------------------------------------------
  3651.  
  3652.     Negative offsets may be specified when relative modes are used. The
  3653.     function lseek returns the new position in the file relative to the
  3654.     beginning of the file (i.e. an absolute offset).
  3655.  
  3656.     || NOTE: offsets are relative.    So, for example, if you want to seek
  3657.     || to the fourth character from the end of the file you would
  3658.     || lseek(fd, -4L, 2);.    Refer to the file_descriptor manual page for
  3659.     || general information.  Unlike file pointers and file handles, the
  3660.     || file descriptor is checked for validity and will simply return an
  3661.     || error if illegal.
  3662.  
  3663.     INPUTS
  3664.     int fd;         file descriptor
  3665.  
  3666.     long offset;        offset relative to how
  3667.  
  3668.     int how;        0 = rel beginning, 1 = rel middle, 2 = rel end
  3669.  
  3670.     RESULTS
  3671.     int newpos;        new position in file (absolute) or < 0 if error
  3672.  
  3673.     SEE ALSO
  3674.     close, creat, fcntl, fdtofh, getfh, ioctl, isatty, mkdir, open, read,
  3675.     rmdir, unlink, write
  3676.  
  3677.     EXAMPLE
  3678.     See open for an example.
  3679.  
  3680. dice/main                                                          dice/main
  3681.  
  3682.     FUNCTION
  3683.     main program entry (ANSI)
  3684.  
  3685.     SYNTAX
  3686.     #include <anything.h>
  3687.     int main(int argc, char **argv)
  3688.     {
  3689.        /* your pride and joy goes here */
  3690.     }
  3691.  
  3692.     DESCRIPTION
  3693.     The main routine is the entry point called after normal
  3694.     initialization of c.lib and the program enviroment is done by the
  3695.     startup module (c.o) and _main routine (in c.lib).  Under ANSI C main
  3696.     is expected to return an integer exit code. You can no longer simply
  3697.     fall through without returning any value.  Returning an exit code
  3698.     from your main routine is exactly the same as exiting with it.
  3699.  
  3700.     || NOTE: Any program run from the WORKBENCH uses a different access
  3701.     || point. Specifically, a program run from the WORKBENCH will run
  3702.     || wbmain instead of main.  Please refer to the manual    page for
  3703.     || wbmain for WORKBENCH operation.
  3704.  
  3705.     If you do not supply a wbmain a dummy wbmain will be supplied by the
  3706.     library which simply exits out of the program.
  3707.  
  3708.     INPUTS
  3709.     int argc;        number of arguments
  3710.  
  3711.     char *argv;        array pointer to arguments
  3712.  
  3713.     SEE ALSO
  3714.     wbmain, _main, exit, _exit
  3715.  
  3716.     EXAMPLE
  3717.     /* Print back out all given arguments */
  3718.     #include <stdio.h>
  3719.  
  3720.     int main(int ac, char**av)
  3721.     {
  3722.        int i;
  3723.        for (i = 0; i < ac; ++i) {
  3724.           printf("Arg #%d = %s\n", i, av[i]);
  3725.        }
  3726.        return(0);
  3727.     }
  3728.  
  3729. dice/_main                                                        dice/_main
  3730.  
  3731.     FUNCTION
  3732.     main program entry, bypass standard c.lib initialization (DICE)
  3733.  
  3734.     SYNTAX
  3735.     #include <the_world.h>
  3736.     void _main(int arglen, char *argptr)
  3737.     {
  3738.         /* your very special code goes here */
  3739.     }
  3740.  
  3741.     DESCRIPTION
  3742.     The _main entry point is called by the startup module (c.o).
  3743.     Normally _main is part of c.lib and does stdio and other
  3744.     initialization before calling the user main routine. _main is
  3745.     responsible for opening the stderr channel as well.  However, if you
  3746.     specify your own _main you will overide the c.lib version.  Normally
  3747.     you either fall through or _exit from _main. A programmable can use
  3748.     the _main entry point when the executable uses nothing but system
  3749.     library routines.  That is, you make no calls to stdio functions such
  3750.     as puts, printf, etc., to low level IO routines such as open, close,
  3751.     read, etc., or malloc or any routine that uses malloc. Self contained
  3752.     routines such as strcpy may still be called, and, of course, you may
  3753.     open any libraries you wish and make library calls.  Since the
  3754.     auto-library openning and closing is done by the startup module
  3755.     (c.o), "dos.library" will still be opened for you automatically if
  3756.     you make any DOS calls.
  3757.  
  3758.     Using the _main entry point usually results in a substantially
  3759.     smaller executable because stdio and other library routines
  3760.     referenced by the c.lib. _main and exit are never referenced and thus
  3761.     never become part of the executable.  It is NOT SUGGESTED that
  3762.     beginning C programmers use the _main entry point.
  3763.  
  3764.     || NOTE: _main is called by the startup module whether the program
  3765.     || was run from the CLI or the WORKBENCH.  You must detect which
  3766.     || yourself and also deal with the WORKBENCH message yourself.
  3767.  
  3768.     int argc;        number of arguments
  3769.  
  3770.     char *argv;        array pointer to arguments
  3771.  
  3772.     SEE ALSO
  3773.     _exit, main, exit
  3774.  
  3775.     EXAMPLE
  3776.     /*
  3777.     **  This program comes in at under 600 bytes.
  3778.     */
  3779.     _main()
  3780.     {
  3781.        Write(Output(), "UG!\n", 4);
  3782.        _exit(0);
  3783.     }
  3784.  
  3785. dice/malloc                                                      dice/malloc
  3786.  
  3787.     FUNCTION
  3788.     allocate memory, the memory is NOT automatically cleared (ANSI)
  3789.  
  3790.     SYNTAX
  3791.     #include <stdlib.h>
  3792.     void *ptr = malloc(bytes);
  3793.     size_t bytes;
  3794.  
  3795.     DESCRIPTION
  3796.     malloc allocates the specified number of bytes of memory. The
  3797.     returned pointer is longword aligned; malloc returns NULL if the
  3798.     memory could not be allocated.
  3799.  
  3800.     || NOTE: Unlike calloc, malloc does not zero the memory before it
  3801.     || returns.
  3802.  
  3803.     INPUTS
  3804.     size_t bytes;        number of bytes to allocate
  3805.  
  3806.     RESULTS
  3807.     void *ptr;        pointer to base of allocated memory.  The memory
  3808.                 is not zero'd.
  3809.  
  3810.     SEE ALSO
  3811.     calloc, strdup
  3812.  
  3813. dice/memcmp                                                      dice/memcmp
  3814.  
  3815.     FUNCTION
  3816.     compare two memory buffers (ANSI)
  3817.  
  3818.     SYNTAX
  3819.     #include <string.h>
  3820.     int r = memcmp(s1, s2, bytes)
  3821.     void *s1;
  3822.     void *s2;
  3823.     size_t bytes;
  3824.  
  3825.     DESCRIPTION
  3826.     memcmp compares two memory buffers.  A byte by byte unsigned
  3827.     comparison is done.  When a comparison fails and the byte in s1 is
  3828.     less than the byte in s2 then -1 is returned.  If the byte in s1 is
  3829.     greater than the byte in s2 then 1 is returned.  If the count is
  3830.     exhausted and all comparisons succeed then 0 is returned indicating
  3831.     the two buffers are the same.
  3832.  
  3833.     INPUTS
  3834.     void *s1;        pointer to first buffer
  3835.  
  3836.     void *s2;        pointer to second buffer
  3837.  
  3838.     size_t bytes;        size of each buffer
  3839.  
  3840.     RESULTS
  3841.     int r;            -1 if buf s1 < buf s2, 0 if buf s1 == buf s2, 1
  3842.                 if buf s1 > buf s2.
  3843.  
  3844.     SEE ALSO
  3845.     memset, setmem, bzero, clrmem, bcopy, bcmp, movmem, memcpy, memmove
  3846.  
  3847.     EXAMPLE
  3848.     #include <stdlib.h>
  3849.     #include <assert.h>
  3850.  
  3851.     main()
  3852.     {
  3853.        unsigned char buf1[5] = {9, 10, 1, 12}; /* 1,729 */
  3854.        unsigned char buf2[5] = {9, 10, 1, 12}; /* 1,729 */
  3855.        int r;
  3856.  
  3857.        r = memcmp(buf1, buf2, 4);
  3858.        assert(r == 0);
  3859.        buf1[2] = 0;
  3860.        r = memcmp(buf1, buf2, 4);
  3861.        assert(r < 0);
  3862.        buf1[2] = 42;
  3863.        r = memcmp(buf1, buf2, 4);
  3864.        assert(r > 0);
  3865.        return(0);
  3866.     }
  3867.  
  3868. dice/memcpy,memmove,movmem,bcopy            dice/memcpy,memmove,movmem,bcopy
  3869.  
  3870.     FUNCTION
  3871.     memcopy: copy memory, ANSI, overlapped memory buffers illegal
  3872.     memmov: copy memory, ANSI, works with overlapped memory buffers
  3873.     movmem: copy memory, UNIX, works with overlapped memory buffers
  3874.     bcopy: copy memory, UNIX, works with overlapped memory buffers
  3875.  
  3876.     SYNTAX
  3877.     #include <string.h>
  3878.     void *ptr = memcpy(d, s, bytes);
  3879.     void *ptr = memmove(d, s, bytes);
  3880.     void *ptr = movmem(s, d, bytes);
  3881.     void *ptr = bcopy(s, d, bytes);
  3882.     void *d;
  3883.     void *s;
  3884.     size_t bytes;
  3885.  
  3886.     DESCRIPTION
  3887.     These functions copy memory from one region to another.  Unlike
  3888.     string routines these functions do not stop the copy when a NULL is
  3889.     encountered.
  3890.  
  3891.     ## WARNING: Be careful about argument ordering.  Some calls  take the
  3892.     ## source buffer first and other calls take the destination buffer
  3893.     ## first.
  3894.  
  3895.     The ANSI committee opted for a destination, source ordering rather
  3896.     than the more logical source, destination ordering. Thus, many
  3897.     programmers will use the non-standard movmem call instead of the ANSI
  3898.     memmove call.  The ANSI function 'memcpy' as defined by the ANSI
  3899.     standard cannot handle overlapped memory areas.  The Amiga
  3900.     implementation can but you should remember this if you intend to port
  3901.     your code.
  3902.  
  3903.     || NOTE: DICE's memory move optimizes the copy using movem when
  3904.     || possible, yielding very fast memory copies for large buffers. The
  3905.     || UNIX bcopy call exists for compatibility purposes and should not
  3906.     || be used with new programs.
  3907.  
  3908.     INPUTS
  3909.     void *s;        source buffer
  3910.  
  3911.     void *d;        destination buffer
  3912.  
  3913.     RESULTS
  3914.     void *ptr;        pointer to the destination buffer(d)
  3915.  
  3916.     SEE ALSO
  3917.     memset, setmem, bzero, clrmem, cmpmem, memcmp
  3918.  
  3919.     EXAMPLE
  3920.     /*
  3921.      * This example copies the entire buffer, not just
  3922.      * the part containing the string.  Normally one just
  3923.      * uses string routines.
  3924.      */
  3925.     #include <string.h>
  3926.     #include <assert.h>
  3927.  
  3928.     main()
  3929.     {
  3930.        char s[16];
  3931.        char d[16];
  3932.        void *p;
  3933.        strcpy(s, "This is a test");
  3934.        p = movmem(s, d, sizeof(s));
  3935.        assert(p == d);
  3936.        puts(d);
  3937.        strcpy(s, "Googolplex");
  3938.        p = bcopy(s, d, sizeof(s));
  3939.        assert(p == d);
  3940.        puts(d);
  3941.        strcpy(s, "EchoBeko");
  3942.        p = memcpy(d, s, sizeof(s));
  3943.        assert(p == d);
  3944.        puts(d);
  3945.        strcpy(s, "GakFuBar");
  3946.        p = memmove(d, s, sizeof(s));
  3947.        assert(p == d);
  3948.        puts(d);
  3949.        return(0);
  3950.     }
  3951.  
  3952. dice/memset,setmem,clrmem,bzero              dice/memset,setmem,clrmem,bzero
  3953.  
  3954.     FUNCTION
  3955.     memset: ANSI, set memory buffer to a byte value
  3956.     setmem: UNIX, set memory buffer to a byte value
  3957.     clrmem: DICE,  zero out a memory buffer
  3958.     bzero: UNIX, zero out a memory buffer
  3959.  
  3960.     SYNTAX
  3961.     #include <string.h>
  3962.     void *ptr = memset(buf, c, n);
  3963.     void *ptr = setmem(buf, n, c);
  3964.     void *ptr = clrmem(buf, n);
  3965.     void *ptr = bzero(buf, n);
  3966.     void *buf;
  3967.     int c;
  3968.     size_t n;
  3969.  
  3970.     DESCRIPTION
  3971.     These functions fill a memory buffer with the specified character c.
  3972.     C is converted to an unsigned chararacter by the fill routine before
  3973.     beginning the fill.  N bytes are filled.
  3974.  
  3975.     ## WARNING: Again, watch out for argument ordering, especially for
  3976.     ## the ANSI memset call.
  3977.  
  3978.     The ANSI committee chose bizarre call ordering so there is another
  3979.     defacto standard call called setmem. The function bzero exists for
  3980.     UNIX compatibility, and clrmem is yet another call (this time
  3981.     introduced by DICE--sorry!). memset and setmem are the most portable
  3982.     calls.
  3983.  
  3984.     INPUTS
  3985.     void *buf;        pointer to buffer to fill
  3986.  
  3987.     int  c;         character to copy into buffer
  3988.  
  3989.     (setmem, memset) size_t n;
  3990.                 # of bytes to fill
  3991.  
  3992.     RESULTS
  3993.     void *ptr;        pointer to buffer (== buf).
  3994.  
  3995.     SEE ALSO
  3996.     malloc, calloc, strdup, movmem, cmpmem
  3997.  
  3998.     EXAMPLE
  3999.     #include <string.h>
  4000.     #include <assert.h>
  4001.     #include <stdlib.h>
  4002.     main()
  4003.     {
  4004.        char buf[32];
  4005.        char *b;
  4006.        b = setmem(buf, 32, 0);
  4007.        assert(b == buf);
  4008.        b = setmem(buf, 4, 'a');
  4009.        b = memset(buf + 4, 'b' , 4);
  4010.        puts(buf);       /*  aaaabbbb    */
  4011.        return(0);
  4012.     }
  4013.  
  4014. dice/mkdir                                                        dice/mkdir
  4015.  
  4016.     FUNCTION
  4017.     create a directory (UNIX)
  4018.  
  4019.     SYNTAX
  4020.     #include <stdio.h>
  4021.     int error = mkdir(dirname)
  4022.     char *dirname;
  4023.  
  4024.     DESCRIPTION
  4025.     mkdir creates a new directory.    It returns 0 if successful, -1 if not
  4026.     (with errno set to an error code).
  4027.  
  4028.     INPUTS
  4029.     char *dirname;        filename of directory to create
  4030.  
  4031.     RESULTS
  4032.     int r;            0 if no error, < 0 if error
  4033.  
  4034.     SEE ALSO
  4035.     close, creat, fcntl, fdtofh, getfh, ioctl, isatty, lseek, mkdir,
  4036.     open, read, rmdir, unlink, write
  4037.  
  4038.     EXAMPLE
  4039.     main()
  4040.     {
  4041.        int r;
  4042.        r = mkdir("T:tmpdir");
  4043.        if (r == 0)
  4044.           puts("Created T:tmpdir successfully");
  4045.        else
  4046.           puts("Unable to create directory T:tmpdir");
  4047.     }
  4048.  
  4049. dice/_mods,_modu                                            dice/_mods,_modu
  4050.  
  4051.     FUNCTION
  4052.     signed/unsigned long modulus 32 (DICE)
  4053.  
  4054.     DESCRIPTION
  4055.     These are assembly functions that DICE uses whenever it needs to do a
  4056.     long modulus.  The functions are not callable from C.
  4057.  
  4058.     INPUTS
  4059.     D0            32 bit signed/unsigned integer
  4060.  
  4061.     D1            32 bit signed/unsigned integer
  4062.  
  4063.     RESULTS
  4064.     D0            Remainder of D0 divided by D1
  4065.  
  4066.     SEE ALSO
  4067.     _divs, _divu, _muls, _mulu
  4068.  
  4069. dice/_muls,_mulu                                            dice/_muls,_mulu
  4070.  
  4071.     FUNCTION
  4072.     signed/unsigned long multiply (DICE)
  4073.  
  4074.     DESCRIPTION
  4075.     These are assembly functions that DICE uses whenever it needs to do
  4076.     long multiplication.  They are not callable from C.
  4077.  
  4078.     INPUTS
  4079.     D0            32 bit signed/unsigned integer
  4080.  
  4081.     D1            32 bit signed/unsigned integer
  4082.  
  4083.     RESULTS
  4084.     D0            D0 multiplied by D1
  4085.  
  4086.     SEE ALSO
  4087.     _divs, _divu, _mods, _modu
  4088.  
  4089. dice/onbreak                                                    dice/onbreak
  4090.  
  4091.     FUNCTION
  4092.     Set special ^C handler (AmigaDOS)
  4093.  
  4094.     SYNTAX
  4095.     typedef int (*fptr)();
  4096.     fptr oldfunc = onbreak(newfunc);
  4097.     fptr newfunc;
  4098.  
  4099.     DESCRIPTION
  4100.     onbreak sets a special function to handle ^C.  It takes a pointer to
  4101.     this function and returns a pointer to the previous onbreak function,
  4102.     if any.  When ^C is hit, the special onbreak function is called
  4103.     before any other action. If the onbreak function returns a non-zero
  4104.     value, ^C aborts the program like it usually does.  If the function
  4105.     returns 0, however, the ^C is completely ignored.
  4106.  
  4107.     INPUTS
  4108.     newfunc         pointer to function or NULL
  4109.  
  4110.     RESULTS
  4111.     fptr oldfunc;        pointer to previous onbreak function
  4112.  
  4113.     SEE ALSO
  4114.     atexit
  4115.  
  4116.     EXAMPLE
  4117.     /*
  4118.     * Note: The reentrancy check is needed because of both
  4119.     * the puts and the sleep() call.
  4120.     */
  4121.     #include <stdio.h>
  4122.     #include <stdlib.h>
  4123.  
  4124.     int brk()
  4125.     {
  4126.        static short cnt = 0; /*  check for reentrancy */
  4127.        if (cnt)         /*  if not 0 then reentered! */
  4128.           return(0);
  4129.           ++cnt;
  4130.        puts("Nah Nah, you can't break me!");
  4131.        sleep(1);
  4132.        --cnt;
  4133.        return(0);
  4134.     }
  4135.  
  4136.     int main()
  4137.     {
  4138.        short i;
  4139.        onbreak(brk);
  4140.        puts("Hit ^C while I loop from 1 to 100.");
  4141.        sleep(2);
  4142.        for (i = 1; i <= 100; ++i)
  4143.        printf("Loop, counting, count = %d\n", i);
  4144.        return(0);
  4145.     }
  4146.  
  4147. dice/open                                                          dice/open
  4148.  
  4149.     FUNCTION
  4150.     open a file (UNIX)
  4151.  
  4152.     SYNTAX
  4153.     #include <fcntl.h>
  4154.     int fd = open(name, modes);
  4155.     char *name;
  4156.     int modes;
  4157.  
  4158.     DESCRIPTION
  4159.     open opens a file of the specified name using the specified modes.
  4160.     The combinations yield different results as described below:
  4161.  
  4162.  
  4163.  O_RDONLY  : open file for reading only
  4164.  ----------+-----------------------------------------------------------------
  4165.  O_WRONLY  : open file for writing only
  4166.  ----------+-----------------------------------------------------------------
  4167.  O_RDWR    : open file for reading and writing
  4168.  ----------+-----------------------------------------------------------------
  4169.  O_NDELAY  : open file non-blocking (not implemented)
  4170.  ----------+-----------------------------------------------------------------
  4171.  O_APPEND  : open file for writing only and force all writes to  append to
  4172.        : the file regardless of the current seek position.    open  is a
  4173.        : lower-level construct than the more standard fopen.
  4174.  ----------+-----------------------------------------------------------------
  4175.  
  4176.  
  4177.     O_CREAT creates the file if it does not exist; O_TRUNC truncates the
  4178.     file if it does exist; O_EXCL is used only with O_CREAT and if the
  4179.     file already exists the open will fail; O_BINARY opens the file for
  4180.     binary reading and writing, vs text.  This flag is ignored by DICE
  4181.     since there is no difference on the Amiga.  However, on Messy-DOS
  4182.     systems, CR-LF must be converted to an LF when reading text files.
  4183.     open returns a descriptor (>= 0) or error (< 0) on failure.
  4184.  
  4185.     || NOTE: Refer to the file_descriptor manual page for general
  4186.     || information. Unlike file pointers and file handles, the file
  4187.     || descriptor is checked for validity and will simply return an error
  4188.     || if illegal.
  4189.  
  4190.     INPUTS
  4191.     char *name;        filename to open
  4192.  
  4193.     long modes;        modes to open the file with
  4194.  
  4195.     RESULTS
  4196.     int fd;         A file descriptor if >= 0, an error if < 0.
  4197.  
  4198.     SEE ALSO
  4199.     close, creat, fcntl, fdtofh, fopen, ioctl, isatty, lseek, mkdir,
  4200.     read, rmdir, unlink, write
  4201.  
  4202.     EXAMPLE
  4203.     #include <fcntl.h>
  4204.     #include <assert.h>
  4205.     main()
  4206.     {
  4207.        int fd, r;
  4208.        fd = open("T:xx", O_WRONLY|O_CREAT|O_TRUNC);
  4209.        assert(fd >= 0);
  4210.        close(fd);
  4211.        fd = open("T:xx", O_CREAT|O_EXCL|O_TRUNC|O_WRONLY);
  4212.        assert(fd < 0);  /* will fail, file already exists */
  4213.        remove("T:xx");
  4214.        fd = open("T:xx", O_CREAT|O_TRUNC|O_WRONLY);
  4215.        assert(fd >= 0); /* will work     */
  4216.        write(fd, "FuBar-", 6);
  4217.        lseek(fd, 0L, 0);  /*  seek back to start of file */
  4218.        r = write(fd, "XxXxx", 5);
  4219.        assert(r == 5);
  4220.        close(fd);
  4221.        fd = open("T:xx", O_APPEND|O_WRONLY);
  4222.        assert(fd >= 0);
  4223.        write(fd, "FuBar\n", 6);
  4224.        close(fd);
  4225.        printf("type t:xx should return \"XxXXX-FuBar\"\n");
  4226.        return(0);
  4227.     }
  4228.  
  4229. dice/perror                                                      dice/perror
  4230.  
  4231.     FUNCTION
  4232.     output error message associated with errno and text to stderr (ANSI)
  4233.  
  4234.     SYNTAX
  4235.     #include <stdio.h>
  4236.     void perror(str);
  4237.     const char *str;
  4238.  
  4239.     DESCRIPTION
  4240.     perror outputs the specified string, a colon, and the error number
  4241.     and error message associated with the current value of errno to
  4242.     stderr, ending with a newline.    Using perror is a common way to
  4243.     generate error messages due to IO failures.
  4244.  
  4245.     INPUTS
  4246.     char *str;        string message to include in error output
  4247.  
  4248.     EXAMPLE
  4249.     #include <stdio.h>
  4250.     main()
  4251.     {
  4252.        FILE *fp = fopen("T:DoesNotExist", "r");
  4253.        if( fp ) {
  4254.           puts("T:DoesNotExist is not supposed to exist!");
  4255.           exit(1);
  4256.        }
  4257.        perror("fopen");
  4258.        return(0);
  4259.     }
  4260.  
  4261. dice/pow,fpow                                                  dice/pow,fpow
  4262.  
  4263.     FUNCTION
  4264.     pow: return one double to the power of another (ANSI)
  4265.     fpow: return one float to the power of another (ANSI)
  4266.  
  4267.     LIBRARY
  4268.     m.lib
  4269.  
  4270.     SYNTAX
  4271.     #include <math.h>
  4272.     double a = pow(b, bp);
  4273.     double bp;
  4274.     double b;
  4275.     float  c = flog(d, dp);
  4276.     float  dp; float  d;
  4277.  
  4278.     DESCRIPTION
  4279.     pow returns one double to the power of another; fpow returns one
  4280.     float to the power of another.
  4281.  
  4282.     INPUTS
  4283.     double b;        double floating point value (base)
  4284.  
  4285.     double bp;        double floating point value (exponent)
  4286.  
  4287.     float d;        float floating point value (base)
  4288.  
  4289.     float dp;        float floating point value (exponent)
  4290.  
  4291.     RESULTS
  4292.     double a;        double floating point value
  4293.  
  4294.     float c;        float floating point value
  4295.  
  4296.     SEE ALSO
  4297.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan
  4298.     facos, fasin
  4299.  
  4300.     EXAMPLE
  4301.     /*
  4302.      *  compile with the math library -lm
  4303.      */
  4304.     #include <math.h>
  4305.     #include <stdio.h>
  4306.     main()
  4307.     {
  4308.        {
  4309.           double a = pow(0.25, 4.0);
  4310.           printf("pow 0.25 ^^ 4.0 = %lf\n", a);
  4311.           /*  0.0039 */
  4312.        }
  4313.        {  /*  less accuracy  */
  4314.           float a = fpow(0.25, 4.0);
  4315.           printf("pow 0.25 ^^ 4.0 = %lf\n", (double)a);
  4316.        }
  4317.        return(0);
  4318.     }
  4319.  
  4320. dice/printf,fprintf,sprintf,vprintf,vfprintf,vsprintf
  4321.  
  4322.     FUNCTION
  4323.     formatted output to stdout, file pointer, or buffer (ANSI)
  4324.  
  4325.     SYNTAX
  4326.     #include <stdio.h>
  4327.     #include <stdarg.h> /* for v[f/s]printf() only */
  4328.     int n = printf(fmt, ...);
  4329.     int n = fprintf(fp, fmt, ...);
  4330.     int n = sprintf(buf, fmt, ...);
  4331.     int n = vprintf(fmt, argvect);
  4332.     int n = vfprintf(fp, fmt, argvect);
  4333.     int n = vsprintf(buf, fmt, argvect);
  4334.     FILE *fp;
  4335.     char *fmt;
  4336.     char *buf;
  4337.     va_list argvect;
  4338.  
  4339.     DESCRIPTION
  4340.     These functions offer formatted printing in various flavors. printf
  4341.     and vprintf output to stdout;  fprintf and vfprintf output to a file
  4342.     pointer (fp); sprintf and vsprintf output to a character buffer.  All
  4343.     routines return the number of characters written if successful, a
  4344.     negative number if not.  Only sprintf and vsprintf are limited in
  4345.     terms of output size (it cannot exceed the buffer you give it). The
  4346.     common argument to all routines is the format specifier.  The format
  4347.     specifier is scanned to determine how to handle the arguments to the
  4348.     call (or the argument list for vfprintf/vsprintf). Characters are
  4349.     copied to the output until a % is encountered. %% indicates a literal
  4350.     '%' character.  Otherwise, the % is followed by a control sequence
  4351.     that tells printf how to output the corresponding argument.  The
  4352.     quantity is output and the scan continues until the end of the format
  4353.     string.  The % formats are interpreted as follows:
  4354.  
  4355.     %[flags][#[.#]][modifier]<conversion-specifier>
  4356.     Items in brackets are optional.  After the %, zero or more flags may
  4357.     be specified.  Then, an optional integer which represents the minimum
  4358.     field width for the object may also be specified.  If an integer is
  4359.     specified it may be followed by a period and another integer that
  4360.     represents that precision with which a number is printed. Zero or
  4361.     more modifiers may then be specified followed by a mandatory
  4362.     conversion specifier.
  4363.  
  4364.     Either or both integers (#[.#]) may be specified as a '*', as in
  4365.     "%*d", specifying that the minimum field width and/or precision is
  4366.     specified as an integer in the argument that occurs before the
  4367.     conversion object.  For example, printf("--%*d--\n", 10, 23); would
  4368.     print the number 23 right justified in a field 10 characters wide.
  4369.  
  4370.     FLAGS
  4371.     -----
  4372.  Minus (-)  Left justify text within the field.
  4373.  
  4374.    <space>  This flag places a blank in front of positive signed numbers, and
  4375.         a minus in front of negative numbers.  This option allows columns
  4376.         of positive and negative numbers to line up.
  4377.  
  4378.   Plus (+)  Cuase a plus sign to be placed before a positive result.  A minus
  4379.         sign is always placed before a negative result.
  4380.  
  4381.  Pound (#)  Format output acording to the type of input.  e, E, f, F always
  4382.         retains the decimal point.    g, G always retains the decimal
  4383.         point, and trailing zeros are kept.  x and X print '0x' and '0X'
  4384.         respectively before the number (except that this is not currently
  4385.         implemented by DICE).
  4386.  
  4387.      0  Pad columns with zeros instead of spaces.  Ignored if a precision
  4388.         is specified or if the '-' flag is specified.
  4389.  
  4390.     MODIFIERS
  4391.     ---------
  4392.      h  Indicates the corresponding integer argument is a short or an
  4393.         unsigned short (stands for "half-size").  Affects scanf(), but
  4394.         since DIEC uses 32 bit integers, has no effect on printf().
  4395.  
  4396.      l  Indicates the corresponding integer argument is a long or
  4397.         unsigned long.  Also indicates floating point argument is a
  4398.         double (else is a float). Under DICE this flag is superfluous for
  4399.         integers, but for portability reasons you want to specify it when
  4400.         an argument is explicitly a long.
  4401.  
  4402.     CONVERSION SPECIFIER
  4403.     --------------------
  4404.      c  Output integer as a character.
  4405.  
  4406.      d  Output a signed integer.
  4407.  
  4408.      e  Output a double quantity in exponential form. The precision
  4409.         specifies the number of digits beyond the decimal point to print,
  4410.         ie: [-]d.dddddde+/-dd.
  4411.  
  4412.      E  Upper case version of e.
  4413.  
  4414.      f  Output a double quantity. The precision specifies the number of
  4415.         digits beyond the decimal point to print, ie:  [-]d.dddddd
  4416.  
  4417.      g  Output a double quantity using either the 'e' or 'f' form,
  4418.         depending on the exponent.
  4419.  
  4420.      G  Output a double quantity using either the 'E' or 'f' form,
  4421.         depending on the exponent.
  4422.  
  4423.      i  Same as 'd', but less portable.
  4424.  
  4425.      n  The argument is a pointer to an integer which is used to set the
  4426.         integer to the bytes written out so far. This is especially
  4427.         useful with sprintf todetermine where a particular part of the
  4428.         format begins in the output buffer.
  4429.  
  4430.      o  The unsigned integer quantity is converted to ASCII-octal.
  4431.  
  4432.      p  The pointer is printed (basically the hexadecimal address is
  4433.         printed).
  4434.  
  4435.      s  The NULL-terminated string represented by the character pointer
  4436.         is printed.
  4437.  
  4438.      u  The unsigned integer quantity is converted to ASCII-decimal.
  4439.  
  4440.      x  The unsigned integer quantity is converted to ASCII-hex using
  4441.         '0'-'9', 'a'-'f'.
  4442.  
  4443.      X  The unsigned integer quantity is converted to ASCII-hex using
  4444.         upper case 'A'-'F' instead of lower case.
  4445.  
  4446.     INPUTS
  4447.     FILE *fp;        file pointer (fprintf, vfprintf)
  4448.  
  4449.     char *fmt;        format string, e.g. "Answer is %d\n"
  4450.  
  4451.     char *buf;        buffer (sprintf, vsprintf)
  4452.  
  4453.     va_list argvect;    arg list (vprintf, vfprintf, vsprintf)
  4454.  
  4455.     RESULTS
  4456.     int n;            Number of characters written if successful, a
  4457.                 negative number if not.  For sprintf and vsprintf
  4458.                 the NULL character at the end of the string is
  4459.                 NOT included in the count.
  4460.  
  4461.     SEE ALSO
  4462.     puts, fputs, fwrite
  4463.  
  4464.     EXAMPLE
  4465.     /*
  4466.     **  Example using most printf conversion specifiers.
  4467.     **  Compile with -lm for the math functions to operate.
  4468.     */
  4469.     #include <stdio.h>
  4470.     #include <stdarg.h>
  4471.     void test_varargs(); /* Always use function prototypes */
  4472.     main()
  4473.     {
  4474.     char buf[256];
  4475.     int  n, i;
  4476.  
  4477.     printf("Normal: ab%c %03d %03o $%p  Fu%s  %u    $%x  $%8X $%08lX\n",
  4478.                'c', 43,  11,  buf, "Bar",32094,4095,4095,4095 );
  4479.  
  4480.     printf("Math:   Double: %le Float: %lf Formatted Float: %2.2lf\n",
  4481.  
  4482.                 1.23E-2,   1.23E-2,           1.257 );
  4483.  
  4484.        printf("%9s\n",    "<-"); /* Specify 9 chracter field width */
  4485.        printf("%*s\n", 9, "<-"); /* Indirect specification */
  4486.  
  4487.        n = sprintf(buf, "Save %n%s", &i, "the whales");
  4488.        puts(buf);
  4489.     printf("String is %d+1 bytes long, with a %%n token at %d.\n", n,
  4490.      i);
  4491.  
  4492.        fprintf(stdout, "\nThis is a call to %s\n", "fprintf");
  4493.  
  4494.        test_varargs("%d %d %d ", 1, 2, 3);
  4495.  
  4496.        return(0);
  4497.     }
  4498.  
  4499.     void test_varargs(ctl, ...)
  4500.     char *ctl;
  4501.     {
  4502.        va_list va;
  4503.        int n;
  4504.        va_start(va, ctl);
  4505.        n = vprintf(ctl, va);
  4506.        printf("<- %d chars written\n", n);
  4507.        va_end(va);
  4508.     }
  4509.  
  4510. dice/putchar                                                    dice/putchar
  4511.  
  4512.     FUNCTION
  4513.     output character to stdout (ANSI)
  4514.  
  4515.     SYNTAX
  4516.     #include <stdio.h>
  4517.     int r = putchar(c); /* MACRO */
  4518.  
  4519.     DESCRIPTION
  4520.     putchar outputs a character to stdout, returning the output character
  4521.     unless an error occured.  If an error occured then EOF is returned.
  4522.  
  4523.     || NOTE: Refer to the file_pointer manual page for general
  4524.     || information.
  4525.  
  4526.     INPUTS
  4527.     int c;            character to output, 0 to 255
  4528.  
  4529.     RESULTS
  4530.     int r;            same as c unless error occured in which case EOF.
  4531.  
  4532.     SEE ALSO
  4533.     putc, fputc, fread, fwrite, getc, getchar
  4534.  
  4535.     EXAMPLE
  4536.     /*
  4537.      * copy stdin to stdout using getchar/putchar. Normally
  4538.      * one uses fread/fwrite, but I'll save that for the
  4539.      * fread manual page.  Note that I output the initial
  4540.      * message to stderr so it does not get stuck into stdout
  4541.      * in case the user has redirected stdout.
  4542.      *
  4543.      * See getc manual page for equivalent example using
  4544.      * getc/putc
  4545.      */
  4546.     #include <stdio.h>
  4547.     main()
  4548.     {
  4549.          int c;
  4550.          fputs("Type a couple of lines, then ^\\ (EOF)\n",
  4551.             stderr);
  4552.          while ((c = getchar()) != EOF)
  4553.          {
  4554.         putchar(c);
  4555.          }
  4556.          return(0);
  4557.     }
  4558.  
  4559. dice/qsort                                                        dice/qsort
  4560.  
  4561.     FUNCTION
  4562.     sort an array of objects (ANSI)
  4563.  
  4564.     SYNTAX
  4565.     #include <stdio.h>
  4566.     #include <stdlib.h>
  4567.     (void) qsort(array, numElem, elemSize, compare_func)
  4568.     void *array;
  4569.     size_t numElem;
  4570.     size_t elemSize;
  4571.     int (*comp_func)(const void *arg1, const void *arg2);
  4572.  
  4573.     DESCRIPTION
  4574.     qsort sorts numElem elements in an array based at array. Each element
  4575.     is elemSize bytes long. When a comparison is required, qsort calls
  4576.     the passed compare_func function pointer with a pointer to the two
  4577.     elements being sorted. DICE currently implements qsort with a simple
  4578.     merge sort algorithm, using relatively slow movmems to avoid having
  4579.     to allocate much additional storage. Very little stack is used.
  4580.     Traditional qsort employs a stack based quick-sort algorithm that
  4581.     might use a massive amount of stack.
  4582.  
  4583.     INPUTS
  4584.     void *array;        pointer to base of array of objects
  4585.  
  4586.     size_t numElem;     number of elements in the array
  4587.  
  4588.     size_t elemSize;    size, in bytes, of each element
  4589.  
  4590.     int (*comp_func)()
  4591.                 function pointer to compare function given
  4592.                 pointers to two of the elements
  4593.  
  4594.     EXAMPLE
  4595.     #include <stdio.h>
  4596.     #include <stdlib.h>
  4597.     #define NUM_GENIUS 8
  4598.     char *StrList[NUM_GENIUS] =
  4599.     {
  4600.        "Euler", "Einstein", "Pascal", "Zeno",
  4601.        "Godel", "Leibniz", "Euclid", "Von Neumann"
  4602.     };
  4603.     my_comp(s1, s2)
  4604.     char **s1; char **s2;
  4605.     {
  4606.        return(strcmp(*s1, *s2));
  4607.     }
  4608.     main()
  4609.     {
  4610.        short i;
  4611.        qsort(StrList, NUM_GENIUS, sizeof(char *), my_comp);
  4612.        for (i = 0; i < NUM_GENIUS; ++i)
  4613.           printf("%d %s\n", i, StrList[i]);
  4614.        return(0);
  4615.     }
  4616.  
  4617. dice/raise                                                        dice/raise
  4618.  
  4619.     FUNCTION
  4620.     raise a signal (cause an 'interrupt' synchronously) (ANSI)
  4621.  
  4622.     SYNTAX
  4623.     int r = raise(signo);
  4624.     int signo;
  4625.  
  4626.     DESCRIPTION
  4627.     raise causes a signal to occur and the appropriate action to be
  4628.     taken.    It returns 0 on success, -1 if the signo is invalid (outside
  4629.     the range of allowed signals).    When you raise a signal, the signal
  4630.     is set back to its default vector before the handler is called.
  4631.     Thus, if you are allowing multiple signals to occur you MUST restore
  4632.     the signal vector with signal from your signal handler before it
  4633.     returns.
  4634.  
  4635.     INPUTS
  4636.     int signo;        signal to cause
  4637.  
  4638.     RESULTS
  4639.     int r;            0 on success, -1 if signo is out of range.
  4640.  
  4641.     SEE ALSO
  4642.     signal
  4643.  
  4644.     EXAMPLE
  4645.     /*
  4646.      *  prints the numbers 0 to 99, except only gets
  4647.      *  to 50 because we 'cause' a ^C.
  4648.      */
  4649.     #include <signal.h>
  4650.     main()
  4651.     {
  4652.        short i;
  4653.        for (i = 0; i < 100; ++i) {
  4654.           printf("i = %d\n", i);
  4655.           if (i == 50)
  4656.           raise(SIGINT);
  4657.         }
  4658.         return(0);
  4659.     }
  4660.  
  4661. dice/rand                                                          dice/rand
  4662.  
  4663.     FUNCTION
  4664.     return pseudo-random number (ANSI)
  4665.  
  4666.     SYNTAX
  4667.     #include <stdio.h>
  4668.     #include <stdlib.h>
  4669.     int n = rand(void);
  4670.     (void) srand(seed)
  4671.     unsigned int seed;
  4672.  
  4673.     DESCRIPTION
  4674.     rand returns a random number as a positive integer ranging from 0 to
  4675.     RAND_MAX.  RAND_MAX is defined in stdlib.h and is at least 32767.
  4676.      DICE implements it as 2147483647 == 0x7FFFFFFF.
  4677.     The initial seed used to generate the pseudo-random sequence is 1,
  4678.     but may be reinitialized to any number you desire using srand. rand
  4679.     is guaranteed to return the same sequence for the same seed.
  4680.  
  4681.     INPUTS
  4682.  
  4683.     none
  4684.  
  4685.     RESULTS
  4686.     int n;            returned by rand(), this number will be a
  4687.                 positive integer.
  4688.  
  4689.     SEE ALSO
  4690.     srand
  4691.  
  4692.     EXAMPLE
  4693.     #include <stdio.h>
  4694.     #include <stdlib.h>
  4695.  
  4696.     main(int ac, char**av)
  4697.     {
  4698.        short i;
  4699.        short j;
  4700.        if (ac == 2) {
  4701.           int seed =
  4702.           strtol(av[1], NULL, 0);
  4703.           srand(seed);
  4704.           printf("using seed = %d\n",
  4705.           seed);
  4706.        }
  4707.        else
  4708.           puts("using seed = 1");
  4709.  
  4710.        for (i = 0; i < 10; ++i) {
  4711.           int n = rand();
  4712.           printf("Random number: %08lx\t(%d)\n", n, n);
  4713.        }
  4714.        for (i = 0; i < 31; ++i) {
  4715.           int isone = 0;
  4716.           long mask = 1 << i;
  4717.           for (j = 0; j < 32767; ++j) {
  4718.          if (rand() & mask) ++isone;
  4719.           }
  4720.           printf("bit %d %5d:%-5d (deviation %d)\n", i,
  4721.               32767 - isone, isone, 16384 -isone);
  4722.        }
  4723.        return(0);
  4724.     }
  4725.  
  4726. dice/read                                                          dice/read
  4727.  
  4728.     FUNCTION
  4729.     read data from a file (UNIX)
  4730.  
  4731.     SYNTAX
  4732.     #include <fcntl.h>
  4733.     int r = read(fd, buf, bytes);
  4734.     int fd;
  4735.     void *buf;
  4736.     int bytes;
  4737.  
  4738.     DESCRIPTION
  4739.     read reads data from a file starting at the current seek position.
  4740.     read returns the number of bytes read or -1 if a read error occurs.
  4741.     With normal files, read will always return the number of bytes
  4742.     requested until the end of file is reached, in which case read may
  4743.     return fewer than the number of bytes requested.  If at the end of a
  4744.     file, read will return 0. With devices read may or may not return the
  4745.     number of bytes requested depending on the device.
  4746.  
  4747.     || NOTE: Refer to the file_descriptor manual page for general
  4748.     || information Unlike file pointers and file handles, the file
  4749.     || descriptor is checked for validity and will simply return an error
  4750.     || if illegal.
  4751.  
  4752.     INPUTS
  4753.     int fd;         file descriptor to read from
  4754.  
  4755.     void *buf;        pointer to buffer to read data into
  4756.  
  4757.     int len;        maximum number of bytes to read
  4758.  
  4759.     RESULTS
  4760.     int r;            number of bytes actually read (could be less than
  4761.                 len or 0), or < 0 if error.
  4762.  
  4763.     SEE ALSO
  4764.     close, creat, fcntl, fdtofh, ioctl, isatty, lseek, mkdir, open, read,
  4765.     rmdir, unlink, write
  4766.  
  4767.     EXAMPLE
  4768.     #include <fcntl.h>
  4769.     #include <assert.h>
  4770.  
  4771.     main()
  4772.     {
  4773.        int fd;
  4774.        int r;
  4775.        char buf[32];
  4776.        fd = open("T:xx", O_WRONLY|O_CREAT|O_TRUNC);
  4777.        assert(fd >= 0);
  4778.        write(fd, "FuBar\n", 6);
  4779.        close(fd);
  4780.        fd = open("T:xx", O_RDONLY);
  4781.        assert(fd >= 0);
  4782.        r = read(fd, buf, sizeof(buf)); /* sizeof(buf)==32 */
  4783.        close(fd);
  4784.        assert(r == 6);
  4785.        /*
  4786.         *  note that the buffer is not terminated with
  4787.         *  a NULL, but since we are using write() which
  4788.         *  requires a length it does not matter
  4789.         */
  4790.        write(1, buf, r);
  4791.     }
  4792.  
  4793. dice/realloc                                                    dice/realloc
  4794.  
  4795.     FUNCTION
  4796.     reallocate memory allocated by calloc, malloc, or strdup (ANSI)
  4797.  
  4798.     SYNTAX
  4799.     #include <stdlib.h>
  4800.     void *newptr = realloc(oldptr, bytes)
  4801.     void *oldptr;
  4802.     size_t bytes;
  4803.  
  4804.     DESCRIPTION
  4805.     realloc reallocates a previously allocated buffer, making it larger
  4806.     or smaller.  It returns a pointer to a new buffer which might be the
  4807.     same as the old buffer, but might not.    Data in the original buffer
  4808.     is copied to the new buffer and the original buffer is freed.  When
  4809.     extending a buffer with realloc note that the extended bytes (beyond
  4810.     the original buffer) will come up garbage.  You may pass a NULL as
  4811.     the first argument to realloc which basically makes realloc a malloc.
  4812.  
  4813.     INPUTS
  4814.     void *oldptr;        pointer to original allocated buffer
  4815.  
  4816.     size_t bytes;        size of new buffer
  4817.  
  4818.     RESULTS
  4819.     void *newptr;        pointer to new buffer
  4820.  
  4821.     SEE ALSO
  4822.     malloc, calloc, strdup
  4823.  
  4824.     EXAMPLE
  4825.     #include <string.h>
  4826.     #include <assert.h>
  4827.     #include <stdlib.h>
  4828.     main()
  4829.     {
  4830.        char *s;
  4831.        int len;
  4832.        s = strdup("This is a test");
  4833.        assert(s);
  4834.        len = strlen(s);
  4835.        /*
  4836.         *  Remember that len does not include the NULL
  4837.         *  byte at the end of the string
  4838.         */
  4839.        s = realloc(s, len + 8);   /*  make more room */
  4840.        assert(s);
  4841.        /*
  4842.         *  we can use strcat since in extending the
  4843.         *  allocated string the NULL *was* copied along
  4844.         *  with the string during the realloc.
  4845.         */
  4846.        strcat(s, "xx");
  4847.        puts(s);       /*  This is a testxx    */
  4848.        return(0);
  4849.     }
  4850.  
  4851. dice/rega4                                                        dice/rega4
  4852.  
  4853.     FUNCTION
  4854.     return current contents of register A4 (DICE)
  4855.  
  4856.     SYNTAX
  4857.     char *basePtr = rega4();
  4858.  
  4859.     DESCRIPTION
  4860.     rega4 is not geta4;  rega4 simply returns the current contents of the
  4861.     A4 register when you need it.  Note that DICE offsets the A4 register
  4862.     32766 from the actual small-data base so as to be able to use the
  4863.     entire -32768 to 32767 range to access 64 kilobytes of small-data.
  4864.     Note that a rega4 call inside a subroutine qualified with __geta4 is
  4865.     guaranteed to return the data base pointer.  Also, a rega4 call from
  4866.     any subroutine not called from an interrupt or a call back will
  4867.     return the proper data base pointer.
  4868.  
  4869.     ## WARNING: Programs which use this function will not be able to be
  4870.     ## made resident.
  4871.  
  4872. dice/remove                                                      dice/remove
  4873.  
  4874.     FUNCTION
  4875.     delete a file (ANSI)
  4876.  
  4877.     SYNTAX
  4878.     #include <stdio.h>
  4879.     int error = remove(filename);
  4880.     const char *filename;
  4881.  
  4882.     DESCRIPTION
  4883.     remove deletes the specified file path returning 0 on success, a
  4884.     negative number on failure.  On the Amiga, an error will occur if you
  4885.     try to delete a file currently opened by yourself or any other
  4886.     process.  remove is an ANSI function.  unlink does the same thing but
  4887.     is a UNIX-compatible function.
  4888.  
  4889.     INPUTS
  4890.     char *filename;     filename to delete
  4891.  
  4892.     RESULTS
  4893.     int error;        0 on success, a negative number on failure
  4894.  
  4895.     SEE ALSO
  4896.     unlink
  4897.  
  4898.     EXAMPLE
  4899.     #include <stdio.h>
  4900.     main()
  4901.     {
  4902.        int error = remove("T:XXX");
  4903.        if (error < 0)
  4904.        {
  4905.           perror("remove(\"T:XXX\") failed");
  4906.           exit(1);
  4907.        }
  4908.        puts("T:XXX has been deleted");
  4909.        return(0);
  4910.     }
  4911.  
  4912. dice/rename                                                      dice/rename
  4913.  
  4914.     FUNCTION
  4915.     rename a file, or move a file from one directory to another on the
  4916.     same filesystem (ANSI)
  4917.  
  4918.     SYNTAX
  4919.     #include <stdio.h>
  4920.     int error = rename(origname, newname);
  4921.     const char *origname;
  4922.     const char *newname;
  4923.  
  4924.     DESCRIPTION
  4925.     rename renames a file.    You may also use rename to move a file (and
  4926.     rename at the same time) to another directory on the same filesystem.
  4927.     rename returns 0 on success, a negative number on failure.
  4928.  
  4929.     INPUTS
  4930.     const char *origname;
  4931.                 existing file const char
  4932.  
  4933.     *newname;        new filename and/or path
  4934.  
  4935.     RESULTS
  4936.     int error;        0 on success, a negative number on failure
  4937.  
  4938.     SEE ALSO
  4939.     rewind
  4940.  
  4941.     EXAMPLE
  4942.     /*
  4943.      *  create the file T:xxjunk and the directory
  4944.      *  T:junkdir then move T:xxjunk into T:junkdir
  4945.      *  and rename to T:yyjunk at the same time.
  4946.      *  As is aptly demonstrated by this example, some
  4947.      *  errors are not really errors.  For example,
  4948.      *  mkdir where the dir already exists is not usually
  4949.      *  an error.
  4950.      */
  4951.     #include <stdio.h>
  4952.     #include <assert.h>
  4953.     #include <errno.h>
  4954.     main()
  4955.     {
  4956.         FILE *fp;     int error;
  4957.         error = mkdir("T:junkdir");
  4958.         if (error < 0 && errno != EEXIST)
  4959.         perror("mkdir");
  4960.         fp = fopen("T:xxjunk", "w");
  4961.         if (fp == NULL) {
  4962.         perror("fopen"); exit(1);
  4963.         }
  4964.         fprintf(fp, "This was originally T:xxjunk!\n");
  4965.         fclose(fp);
  4966.  
  4967.         error = rename("T:xxjunk", "T:junkdir/yyjunk");
  4968.         if (error < 0)
  4969.            perror("Error renaming T:xxjunk");
  4970.         else
  4971.            puts("rename succeeded, look in T:junkdir");
  4972.         return(0);
  4973.     }
  4974.  
  4975. dice/rewind                                                      dice/rewind
  4976.  
  4977.     FUNCTION
  4978.     seek filepointer to beginning of file (ANSI)
  4979.  
  4980.     SYNTAX
  4981.     #include <stdio.h>
  4982.     void rewind(fp);
  4983.     FILE *fp;
  4984.  
  4985.     DESCRIPTION
  4986.     rewind rewinds the file to the beginning, equivalent to fseek(fp, 0L,
  4987.     0);.
  4988.  
  4989.     || NOTE: Refer to the file_pointer manual page for general
  4990.     || information.
  4991.  
  4992.     INPUTS
  4993.     FILE *fp;        file pointer to rewind
  4994.  
  4995.     SEE ALSO
  4996.     fseek, fgetpos, fsetpos
  4997.  
  4998.     EXAMPLE
  4999.     /* print a file 3 times */
  5000.     #include <stdio.h>
  5001.     main(int ac, char**av) {
  5002.        FILE *fp;
  5003.        int i;
  5004.        char buf[256];
  5005.        if (ac == 1) {
  5006.           puts("Expected textfile argument");
  5007.           exit(1);
  5008.        }
  5009.        fp = fopen(av[1], "r");
  5010.        if (fp == NULL) {
  5011.           printf("Unable to open %s\n", av[1]);
  5012.           exit(1);
  5013.        }
  5014.        for (i = 1; i <= 3; ++i) {
  5015.           rewind(fp); printf("PRINTING #%d\n", i);
  5016.           while (fgets(buf, sizeof(buf), fp))
  5017.          fputs(buf, stdout);
  5018.        }
  5019.        return(0);
  5020.     }
  5021.  
  5022. dice/rmdir                                                        dice/rmdir
  5023.  
  5024.     FUNCTION
  5025.     delete a directory (UNIX)
  5026.  
  5027.     SYNTAX
  5028.     #include <stdio.h>
  5029.     int r = rmdir(dirname);
  5030.     char *dirname;
  5031.  
  5032.     DESCRIPTION
  5033.     rmdir deletes a directory.  The directory must be empty for the
  5034.     deletion to work.  On the Amiga this call is equivalent to remove or
  5035.     unlink.
  5036.  
  5037.     INPUTS
  5038.     char *dirname;        name of directory to delete
  5039.  
  5040.     RESULTS
  5041.     int r;            0 if successful, non-zero if error
  5042.  
  5043.     SEE ALSO
  5044.     mkdir
  5045.  
  5046.     EXAMPLE
  5047.     #include <assert.h>
  5048.     main()
  5049.     {
  5050.        int r;
  5051.        r = mkdir("T:tmpdir");
  5052.        assert(r == 0);
  5053.        r = rmdir("T:tmpdir");
  5054.        assert(r == 0);
  5055.     }
  5056.  
  5057. dice/scanf,fscanf,sscanf                            dice/scanf,fscanf,sscanf
  5058.  
  5059.     FUNCTION
  5060.     scan formatted text and convert to variables (ANSI)
  5061.  
  5062.     SYNTAX
  5063.     #include <stdio.h>
  5064.     int n = scanf(ctl, ...);      /* Input: standard in */
  5065.     int n = fscanf(fp, ctl, ...); /* Input: file pointer */
  5066.     int n = sscanf(str,ctl, ...); /* Input: string buffer */
  5067.     const char *ctl;
  5068.     FILE *fp;
  5069.     char *str;
  5070.  
  5071.     DESCRIPTION
  5072.     These functions scan the specified input file or buffer for fields
  5073.     matching those specified by the control field. Commands starting with
  5074.     % relate the scanned text to a list of pointers. The values the
  5075.     pointers point to are updated (say that three times fast). Other
  5076.     characters in the control field must match the scanned text exactly,
  5077.     except for white space charaters, which match any length of white
  5078.     space in the scanned text.
  5079.  
  5080.  
  5081.     Control Field Commands
  5082.     ----------------------
  5083.     The control field command format is as follows:
  5084.  
  5085.     %[*][nnn][h/l/L]<conversion-specifier>
  5086.  
  5087.      %  All conversion specifiers start with %.  Use %% to match a single
  5088.         % in the source text.
  5089.  
  5090.      *  An optional * tells scanf to perform the conversion, but discard
  5091.         the result. Do not include a destination pointer for skiped
  5092.         conversions.
  5093.  
  5094.        nnn  An optional decimal number is used for string conversion to
  5095.         specify a maximum field width.
  5096.  
  5097.      h  The modifier h specifies the destination should be treated as a
  5098.         short integer rather, than an integer.  The h conversion applies
  5099.         to d, i, n, o, u, x and X.
  5100.  
  5101.      l  The modifier l specifies the destination should be treated as a
  5102.         long integer, rather than an integer.  The l conversion applies
  5103.         to d, i, o, u, x and X.
  5104.  
  5105.      L  When used with formats e, f or g, specifies the destination
  5106.         should be treated to a long double rather than a double.
  5107.  
  5108.     Conversion Specifiers
  5109.     ---------------------
  5110.      c  With no field width specified, converts one character.  With a
  5111.         field width, converts the number of characters specified by the
  5112.         field width into an array. The expected argument is a pointer to
  5113.         a character or array of characters. No white space is skipped.
  5114.  
  5115.      d  Converts a decimal (base 10) number.  The expected argument is a
  5116.         pointer to an integer.  The l and h modifiers may be used to
  5117.         specify either a long or short integer result.
  5118.  
  5119.      e/f/g  Scans a floating point number.  If the l modifier is used, a long
  5120.         double pointer is expected (not implemented in DICE yet), else a
  5121.         double is expected.
  5122.  
  5123.      i  Converts a number to an integer.  The format should be equivalent
  5124.         to that expected by strtol with a base argument of 0 (automatic
  5125.         detection of format).
  5126.  
  5127.      n  This command does not scan any text.  It places into an integer
  5128.         the current count of processed characters.
  5129.  
  5130.      o  An octal number is expected.  The value is stored to an integer.
  5131.  
  5132.      p  A pointer is expected, in the same format as the %p from printf.
  5133.         The value is stored into a pointer, passed as void ** (a pointer
  5134.         to any type of pointer).
  5135.  
  5136.      s  Reads a string of non-white space characters and copies into the
  5137.         array specified by the argument.  The argument must be of type
  5138.         char * and have enough space to handle any possible string plus a
  5139.         NULL terminator.
  5140.  
  5141.      x  A hexadecimal (base 16) number is expected.  The value is stored
  5142.         into an integer.  This is equivalent to calling strtol with a
  5143.         base of 16.
  5144.  
  5145.     %%  Match a single % in the input stream.
  5146.  
  5147.      [...]  Scan a scanset.  Scan the input stream and place the characters
  5148.         into a char * buffer until a character is read that does not
  5149.         match the scanset.    If a scan set begins with ^ (as in [^abcd])
  5150.         then all characters are allowed except those specified in the
  5151.         scanset.  If a scanset begins as []abcd] or [^]abcd] then the ']'
  5152.         character is included in the scan set and the set is terminated
  5153.         by ']'.
  5154.  
  5155.         || Note: All scanf arguments are pointers.    A common mistake is
  5156.         || to pass arguments directly; pointers are required.  All
  5157.         || floating point arguments must be pointers to doubles.
  5158.  
  5159.     INPUTS
  5160.     char *ctl;        format control string, containing % commands
  5161.  
  5162.     FILE *fp;        file pointer for fscanf
  5163.  
  5164.     char *str;        string pointer for sscanf
  5165.  
  5166.     ...            pointers to variables ready to receive
  5167.                 conversions.
  5168.  
  5169.     RESULTS
  5170.     int n;            Number of conversions that occured or -1 if no
  5171.                 conversions could be made (this usually means
  5172.                 EOF).  The function may return less than the
  5173.                 number of requested conversions.  This value does
  5174.                 not count any %* conversions.
  5175.  
  5176.     SEE ALSO
  5177.     sprintf, printf, fprintf, strtol
  5178.  
  5179.     EXAMPLE
  5180.     #include <stdio.h>
  5181.     main(int ac, char**av)
  5182.     {
  5183.     int    n, integer;
  5184.  
  5185.        n = sscanf("Input: 132\n", "Input: %ld\n", &integer);
  5186.        printf("First test: n=%d integer=%d  ", n, integer);
  5187.  
  5188.     short a[3] = {-1, -2, -3}; // Unscanned arguments are NOT
  5189.     int   b[3] = {-4, -5, -6}; // cleared - they retain
  5190.     char  buf1[11], buf2[11];  // former values!
  5191.  
  5192.        buf1[0]=buf2[0]=buf2[10] = 0;
  5193.  
  5194.        if (ac != 2) {
  5195.           puts("Expected string to format!"); exit(1);
  5196.        }
  5197.        n = sscanf(av[1],"%hd %ho %hi %*d %X %n%10s %10c   -%d-",
  5198.                  a+0,a+1,a+2,   b+0,b+1,buf1,buf2,b+2);
  5199.        printf("Second test: %d elements\n", n);
  5200.        printf("a (shorts): %d %d %d\n", a[0], a[1], a[2]);
  5201.        printf("b (ints)  : %d %d %d\n", b[0], b[1], b[2]);
  5202.        printf("buf1      : %s\nbuf2      : %s\n", buf1, buf2);
  5203.        return(0);
  5204.     }
  5205.     /* scanf "100 100 0x100 200 0x300 reduce reuse recycle!"
  5206.     ** First test: n=1 integer=132    Second test: 6 elements
  5207.     ** a (shorts): 100 64 256
  5208.     ** b (ints)  : 768 24 -6
  5209.     ** buf1      : reduce
  5210.     ** buf2      : reuse recy
  5211.     */
  5212.  
  5213. dice/setbuf,setvbuf                                      dice/setbuf,setvbuf
  5214.  
  5215.     FUNCTION
  5216.     change a file pointer's buffering (ANSI)
  5217.  
  5218.     SYNTAX
  5219.     #include <stdio.h>
  5220.     void setbuf(fp, buf);
  5221.     error = setvbuf(fp, buf, mode, size);
  5222.     FILE *fp; char *buf;
  5223.     int mode; size_t size; int error;
  5224.  
  5225.     DESCRIPTION
  5226.     setbuf changes the internal buffer used by stdio.  The buffer you
  5227.     pass it must be BUFSIZ bytes in size.  You can set a file pointer to
  5228.     unbuffered by passing NULL for your buffer.
  5229.  
  5230.     setvbuf supersedes this call and is, in general, a better function.
  5231.     Type type argument must be one of _IOFBF (Fully Buffered), _IOLBF
  5232.     (Line Buffered) or _IONBF (Non Buffered).  For use with DICE it is
  5233.     best to supply a buffer size, but a NULL buf pointer. DICE will
  5234.     allocate buffers as they are needed.
  5235.  
  5236.     ## WARNING: If buffering is turned off for a file pointer
  5237.     ## representing a console device, the console device is set to
  5238.     ## unbuffered as well.    If buffering is turned on for a file pointer
  5239.     ## representing a console device, the console device is set to
  5240.     ## buffered as well.
  5241.  
  5242.     || NOTE: Refer to the file_pointer manual page for general
  5243.     || information.
  5244.  
  5245.     INPUTS
  5246.     FILE *fp;        file pointer to affect.
  5247.  
  5248.     char *buf;        buffer for use by the file pointer, or NULL.
  5249.  
  5250.     int type;        (setvbuf) type of buffering.
  5251.  
  5252.     size_t size;        (setvbuf) requested buffer size or zero.
  5253.  
  5254.     int error;        (setvbuf) error code (if size or type are
  5255.                 invalid).
  5256.  
  5257.     EXAMPLE 1
  5258.     #include <stdio.h>
  5259.     main()
  5260.     {
  5261.     FILE *fp;
  5262.  
  5263.        fp = fopen("CON:0/0/640/100/DICE Setbuf Test","w");
  5264.        if(! fp )
  5265.           exit( 10 );
  5266.  
  5267.        fprintf(fp, "This will be buffered, ");
  5268.        sleep(2);
  5269.        setvbuf(fp, NULL, _IONBF, 0);
  5270.        fprintf(fp, "yet these will print immediately ");
  5271.        sleep(2);
  5272.        fprintf(fp, "(we are unbuffered).");
  5273.        sleep(4);
  5274.        fprintf(fp, "\n");
  5275.  
  5276.        setvbuf(fp, NULL, _IOLBF, 128);
  5277.        fprintf(fp, "Now back to buffered... ");
  5278.        sleep(2);
  5279.        fprintf(fp, "(See - you had to wait).\n");
  5280.        sleep(8);
  5281.  
  5282.        fclose( fp );
  5283.        return(0);
  5284.     }
  5285.  
  5286.     EXAMPLE 2
  5287.     #include <stdio.h>
  5288.     #define BUFFER_SIZE 128
  5289.     main()
  5290.     {
  5291.        int c;    char buf[256];
  5292.  
  5293.        setvbuf(stdin, NULL, _IONBF, 0);
  5294.        printf("Type a character: ");
  5295.        fflush(stdout);
  5296.        c = getchar();
  5297.        printf("c = %d\n", c);
  5298.  
  5299.        setvbuf(stdin, NULL, _IOLBF, BUFFER_SIZE);
  5300.        printf("Type a line: ");
  5301.        fflush(stdout);
  5302.        fgets(buf, sizeof(buf), stdin);
  5303.        printf("You said   : %s\n", buf);
  5304.        return(0);
  5305.     }
  5306.  
  5307. dice/setjmp,longjmp                                      dice/setjmp,longjmp
  5308.  
  5309.     FUNCTION
  5310.     setjmp: save procedure context for future long jump (ANSI)
  5311.     longjmp: jump to a previously saved procedure context (ANSI)
  5312.  
  5313.     SYNTAX
  5314.     #include <setjmp.h>
  5315.     int r = setjmp(enviro);
  5316.     (void) longjmp(enviro, rval)
  5317.     jmp_buf enviro;
  5318.     int rval;
  5319.  
  5320.     DESCRIPTION
  5321.     First, setjmp stores the current procedure context into an
  5322.     environment array whose type is jmp_buf.  When called by a procedure
  5323.     it saves the environment and returns 0.  Then when called, longjmp
  5324.     jumps to a previously saved environment causing execution to begin at
  5325.     the setjmp call that saved that enviroment. Yet instead of returning
  5326.     0 the 'resumed' setjmp returns a return value set by the longjmp
  5327.     call.  Jmp_buf is a typedef of an array, thus by passing a jmp_buf
  5328.     structure we really pass the address of it.  setjmp and longjmp are
  5329.     fully compatible with dynamic stacks  (-gs option to DCC).
  5330.  
  5331.     :: Beginner's Note: setjmp and longjmp should be used sparingly, if
  5332.     :: at all. They make code hard to understand, are rife with potential
  5333.     :: for adding bugs and are considered even uglier than the C goto
  5334.     :: statement.
  5335.  
  5336.     In the example below main is still stacked when the longjmp occurs
  5337.     and is thus valid.  It would be illegal, for instance, to call a
  5338.     subroutine which does a setjmp and RETURNS to you, then longjmp back
  5339.     to that subroutine. The contents of the jmp_buf structure are private
  5340.     and may not be modified by the program.
  5341.  
  5342.     ## WARNING: You can only longjmp to a previously saved enviroment
  5343.     ## that has not been unstacked.  WARNING: setjmp saves the current
  5344.     ## state of the registers, but not any registers that get modified
  5345.     ## between the setjmp and the longjmp!
  5346.  
  5347.     Thus, auto variables placed in registers (which is done so
  5348.     automatically under DICE) may contain 'old' values after a longjmp if
  5349.     they were modified after the setjmp. To prevent this such variables
  5350.     must, by ANSI convention, be made volatile.  The volatile qualifier
  5351.     forces an auto variable to be placed on the stack instead of in a
  5352.     register.
  5353.  
  5354.     INPUTS
  5355.     jmp_buf enviro;     enviroment structure
  5356.  
  5357.     int rval;        return value (longjmp call)
  5358.  
  5359.     RESULTS
  5360.     (setjmp) int r;     0 when called directly, rval when enviroment
  5361.                 restored by a longjmp
  5362.  
  5363.     SEE ALSO
  5364.     onbreak, signal
  5365.  
  5366.     EXAMPLE
  5367.     #include <stdio.h>
  5368.     #include <setjmp.h>
  5369.     jmp_buf x;
  5370.  
  5371.     int  brk(void);
  5372.     void breakme(void);
  5373.  
  5374.     main()
  5375.     {
  5376.        int r;
  5377.        onbreak(brk);
  5378.  
  5379.        r = setjmp(x); // returns 0 when called by main
  5380.        puts("\nArnie sez:  I'll be back...");
  5381.  
  5382.        if (r == 0)
  5383.           for (;;)
  5384.         breakme();
  5385.  
  5386.        printf("Broke and jumped, r = %d\n", r);
  5387.        return(0);
  5388.     }
  5389.     /* even though the onbreak call is supposed to return,
  5390.     ** we can longjmp out of it as well.
  5391.     */
  5392.     int brk()
  5393.     {
  5394.        longjmp(x, 23);
  5395.     }
  5396.     void breakme()
  5397.     {
  5398.        puts("Break Me With ^C!");
  5399.     }
  5400.  
  5401. dice/signal                                                      dice/signal
  5402.  
  5403.     FUNCTION
  5404.     set a signal vector for a signal (ANSI)
  5405.  
  5406.     SYNTAX
  5407.     #include <signal.h>
  5408.     typedef void (*__sigfunc)(int);
  5409.     __sigfunc oldfunc = signal(signo, newfunc)
  5410.     int signo;
  5411.     __sigfunc newfunc;
  5412.  
  5413.     DESCRIPTION
  5414.     signal sets a signal vector function for a given signal number as
  5415.     defined in <signal.h> and returns the previously set function.
  5416.     Currently only SIGINT causes any semi-asynchronous action to occur.
  5417.     You may pass newfunc as your own signal function or one of:
  5418.  
  5419.    SIG_ERR  error (exit program)
  5420.  
  5421.    SIG_DFL  default (for break, normal operation)
  5422.  
  5423.    SIG_IGN  ignore signal (for break, ^C is now ignored)
  5424.  
  5425.         when a signal occurs, the signal is set back to its default
  5426.         condition before the handler is called.  Thus, if you are
  5427.         allowing multiple signals to occur you must restore the signal
  5428.         vector with signal from your signal handler before it returns.
  5429.  
  5430.         || NOTE: On the Amiga, signals are not truly asynchronous.    ^C is
  5431.         || detected during stdio calls only.  No other signal is
  5432.         || implemented though you CAN modify any signal vector 0 to 31
  5433.         || and raise it with the raise call. Early versions of DICE,
  5434.         || including quite possibly this version, do not understand
  5435.         || complex type declarations containing procedural types. Thus,
  5436.         || you may have to get around the problem by building up a
  5437.         || complex procedural type with typedefs.  Unlike onbreak, a
  5438.         || signal function returns no value.
  5439.  
  5440.     INPUTS
  5441.     int signo;        signal to modify, usually SIGINT __sigfunc
  5442.  
  5443.     newfunc;        signal function or SIG_ERR, SIG_DFL, SIG_IGN
  5444.  
  5445.     RESULTS
  5446.     __sigfunc oldfunc;
  5447.                 previous signal function
  5448.  
  5449.     SEE ALSO
  5450.     raise
  5451.  
  5452.     EXAMPLE
  5453.     #include <signal.h>
  5454.     void brkfunc(int);
  5455.     main()
  5456.     {
  5457.        short i;
  5458.        puts("The following is unbreakable");
  5459.        sleep(1);
  5460.        signal(SIGINT, SIG_IGN);
  5461.        for (i = 0; i < 100; ++i) printf("1 %d\n", i);
  5462.        puts("The following may be broken out of");
  5463.        puts("with a cute message");
  5464.        sleep(1);
  5465.        signal(SIGINT, brkfunc);
  5466.        for (i = 0; i < 100; ++i) printf("2 %d\n", i);
  5467.        puts("The following may be broken out of");
  5468.        sleep(1);
  5469.        signal(SIGINT, SIG_DFL);
  5470.        for (i = 0; i < 100; ++i) printf("3 %d\n", i);
  5471.        puts("Hey! You never hit ^C! \
  5472.          What kind of test is this!");
  5473.        return(0);
  5474.     }
  5475.     void brkfunc(int signo)
  5476.     {
  5477.        printf("signo %d. Want cute?  Get a kitten.\n", signo);
  5478.        exit(1);
  5479.     }
  5480.  
  5481. dice/sin,fsin                                                  dice/sin,fsin
  5482.  
  5483.     FUNCTION
  5484.     sin: return sine of a double quantity (ANSI)
  5485.     fsin: return sine of a float quantity (ANSI)
  5486.  
  5487.     LIBRARY
  5488.     m.lib
  5489.  
  5490.     SYNTAX
  5491.     #include <math.h>
  5492.     double a = sin(b);
  5493.     double b;
  5494.     float  c = fsin(d);
  5495.     float  d;
  5496.  
  5497.     DESCRIPTION
  5498.     sin returns the sine of a double quantity; fsin returns the sine of a
  5499.     floating point quantity.  These functions use radian measure ("rad"
  5500.     on your calulator).
  5501.  
  5502.     INPUTS
  5503.     double b;        double floating point value
  5504.  
  5505.     float d;        float floating point value
  5506.  
  5507.     RESULTS
  5508.     double a;        result double floating point value
  5509.  
  5510.     float c;        result float floating point value
  5511.  
  5512.     SEE ALSO
  5513.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sqrt, tan, facos,
  5514.     fasin
  5515.  
  5516.     EXAMPLE
  5517.     See cos for an example.
  5518.  
  5519. dice/sleep                                                        dice/sleep
  5520.  
  5521.     FUNCTION
  5522.     sleep for a period of time (UNIX)
  5523.  
  5524.     SYNTAX
  5525.     #include <stdio.h>
  5526.     sleep(n);
  5527.     int n;
  5528.  
  5529.     DESCRIPTION
  5530.     The sleep function waits for a period of time specified in seconds.
  5531.     It can be interrupted by a ^C.
  5532.  
  5533.     || NOTE: The timekeeping of sleep is not very accurate.  On the
  5534.     || Amiga, sleep is implemented with a loop of delay(50); calls.
  5535.  
  5536.     INPUTS
  5537.     int n;            number of seconds to sleep
  5538.  
  5539.     EXAMPLE
  5540.     #include <stdio.h>
  5541.     main(int ac, char**av)
  5542.     {
  5543.        puts("Sleeping for 10 seconds");
  5544.        sleep(10);
  5545.        puts("That was a good rest");
  5546.        return(0);
  5547.     }
  5548.  
  5549. dice/sqrt,fsqrt                                              dice/sqrt,fsqrt
  5550.  
  5551.     FUNCTION
  5552.     sqrt: return the square root of a double (ANSI)
  5553.     fsqrt: return the square root of a float (ANSI)
  5554.  
  5555.     LIBRARY
  5556.     m.lib
  5557.  
  5558.     SYNTAX
  5559.     #include <math.h>
  5560.     double a = sqrt(b);
  5561.     double b;
  5562.     float  c = fsqrt(d);
  5563.     float  d;
  5564.  
  5565.     DESCRIPTION
  5566.     sqrt returns the square root of a double quantity; fsqrt returns the
  5567.     square root of a floating point quantity.
  5568.  
  5569.     INPUTS
  5570.     double b;        double floating point value
  5571.  
  5572.     float d;        float floating point value
  5573.  
  5574.     RESULTS
  5575.     double a;        double floating point value
  5576.  
  5577.     float c;        float floating point value
  5578.  
  5579.     SEE ALSO
  5580.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, tan, facos,
  5581.     fasin
  5582.  
  5583.     EXAMPLE
  5584.     /*
  5585.      *  compile with the math library -lm
  5586.      */
  5587.     #include <math.h>
  5588.     #include <stdio.h>
  5589.     main()
  5590.  
  5591.     {
  5592.        {
  5593.           double a = sqrt(0.25);
  5594.           printf("sqrt 0.25 = %lf\n", a);
  5595.           /* 0.5000 */
  5596.        }
  5597.        {  /*  less accuracy  */
  5598.           float a = fsqrt(0.25);
  5599.           printf("sqrt 0.25 = %lf\n", (double)a);
  5600.        }
  5601.        return(0);
  5602.     }
  5603.  
  5604. dice/srand                                                        dice/srand
  5605.  
  5606.     FUNCTION
  5607.     set seed for pseudo-random number (ANSI)
  5608.  
  5609.     SYNTAX
  5610.     #include <stdio.h>
  5611.     #include <stdlib.h>
  5612.     (void) srand(seed)
  5613.     unsigned int seed;
  5614.  
  5615.     DESCRIPTION
  5616.     srand initializes the seed for function rand.
  5617.  
  5618.     INPUTS
  5619.     unsigned int seed;
  5620.                 an unsigned integer used to seed the
  5621.                 pseudo-random
  5622.  
  5623.     number generator via srand.
  5624.  
  5625.     SEE ALSO
  5626.     rand
  5627.  
  5628.     EXAMPLE
  5629.     See the "rand" manual page for an example
  5630.  
  5631. dice/stack_abort                                            dice/stack_abort
  5632.  
  5633.     FUNCTION
  5634.     exit point when dynamic stack allocation fails (DICE)
  5635.  
  5636.     SYNTAX
  5637.     void stack_abort(void)
  5638.     {
  5639.         /* .. your exit code .. */
  5640.         abort();
  5641.     }
  5642.  
  5643.     DESCRIPTION
  5644.     When dynamic stack allocation is enabled via the -gs option and such
  5645.     an allocation fails, stack_abort is called.  If you do not specify a
  5646.     stack_abort routine, the c.lib stack_abort function will simply call
  5647.     abort.
  5648.  
  5649.     If you do specify a stack_abort routine, you have two choices: you
  5650.     can exit out of the program, or you can simply return from the
  5651.     subroutine which retries the allocation and calls stack_abort again
  5652.     if it fails (perhaps wait a bit in hopes memory has become
  5653.     available).
  5654.  
  5655.     The program has about 2KB of stack left at the time this function is
  5656.     called.  Since a low memory condition exists when this function is
  5657.     called you should not do anything that might require additional
  5658.     allocations!
  5659.  
  5660.     SEE ALSO
  5661.     abort, exit
  5662.  
  5663. dice/stat                                                          dice/stat
  5664.  
  5665.     FUNCTION
  5666.     stat a file by name (UNIX)
  5667.  
  5668.     SYNTAX
  5669.     #include <sys/stat.h>
  5670.     int error = stat(name, &stat_buf);
  5671.     const char *name;
  5672.     struct stat stat_buf;
  5673.  
  5674.     DESCRIPTION
  5675.     stat is a UNIX-compatible call that returns information pertaining to
  5676.     the file represented by its name. If 0 is returned, stat succeeded
  5677.     and the fields will be filled in as follows:
  5678.  
  5679.    st_mode  Flags S_IFDIR if directory, S_IFREG if regular file, S_IREAD if
  5680.         readable, S_IWRITE if writable, S_IEXEC if executable.
  5681.  
  5682.    st_size  Size of the file, in bytes.
  5683.  
  5684.  st_blksize
  5685.         Always returns 512 (for now).
  5686.  
  5687.  st_blocks  A guess at the number of actual blocks the file takes up,
  5688.         including headers and side sectors.
  5689.  
  5690.   st_ctime  Time the file was last modified.
  5691.  
  5692.   st_mtime  Same as st_ctime.
  5693.  
  5694.     st_dev  Physical device ID (do not try to interpret this field, but it
  5695.         does represent the DOS handler).
  5696.  
  5697.     st_ino  inode ID (usually a file block number on the Amiga).
  5698.  
  5699.         || NOTE: On the Amiga one normally cannot directly examine a file
  5700.         || that is exclusively locked.  If this case occurs, stat will
  5701.         || attempt to scan the parent directory for the file and if that
  5702.         || doesn't work, returns -1.
  5703.  
  5704.     INPUTS
  5705.     char *name;        name of file to stat
  5706.  
  5707.     struct stat *sbuf;
  5708.                 address of stat structure that will be filled in
  5709.  
  5710.     RESULTS
  5711.     int error;        0 on success, < 0 on error
  5712.  
  5713.     SEE ALSO
  5714.     chdir
  5715.  
  5716.     EXAMPLE
  5717.     #include <stdio.h>
  5718.     #include <fcntl.h>
  5719.     #include <sys/stat.h>
  5720.     main(int ac, char**av)
  5721.     {
  5722.        int r;
  5723.        struct stat stat_buf;
  5724.        if (ac == 1) {
  5725.           puts("Expected a test file name");
  5726.           exit(1);
  5727.        }
  5728.        r = stat(av[1], &stat_buf);
  5729.        if (r < 0)
  5730.           printf("Can't stat %s\n", av[1]);
  5731.        else {
  5732.           printf("File is %d bytes long\n", stat_buf.st_size);
  5733.           printf("modified %s", ctime(&stat_buf.st_ctime));
  5734.        }
  5735.        return(0);
  5736.     }
  5737.  
  5738. dice/stdin,stdout,stderr,EOF                    dice/stdin,stdout,stderr,EOF
  5739.  
  5740.     FUNCTION
  5741.     stdin: standard input channel (file pointer - MACRO)
  5742.     stdout: standard output channel (file pointer - MACRO)
  5743.     stderr: standard error channel (file pointer - MACRO)
  5744.     EOF: special value representing End-Of-File
  5745.  
  5746.     SYNTAX
  5747.     #include <stdio.h>
  5748.  
  5749.     DESCRIPTION
  5750.     stdin is the name for the program's standard input stream, a "FILE *"
  5751.     (Pointer to structure FILE).  This can be redirected via command line
  5752.     redirection when the program is run.  stdout is a FILE * type that
  5753.     represents the program's standard output stream.  This can also be
  5754.     redirected via command line redirection when the program is run.
  5755.     stderr is a FILE * type that represents the program's standard error
  5756.     stream. Currently stderr is opened by _main and represents the
  5757.     console device associated with the program regardless of standard
  5758.     redirections.  EOF is special value, -1 in this implementation, that
  5759.     represents an end of file marker.  Functions that return a single
  5760.     character actually return type int to allow EOF to be distinct from
  5761.     valid characters. Include the file stdio.h to use these defintions.
  5762.  
  5763.     These file pointers may be fclose'd or freopen'd at any time. The
  5764.     stdio macros getchar and putchar and stdio library routines gets and
  5765.     puts deal with stdin and stdout respectively while other library
  5766.     routines such as perror output to stderr.
  5767.  
  5768. dice/stpchr                                                      dice/stpchr
  5769.  
  5770.     FUNCTION
  5771.     Search for a character in a string (UNIX)
  5772.  
  5773.     SYNTAX
  5774.     #include <string.h>
  5775.     char *ptr = stpchr(s, c)
  5776.     const char *s;
  5777.     int c;
  5778.  
  5779.     DESCRIPTION
  5780.     This searches for the character c within the string pointed to by s.
  5781.     The terminating NULL at the end of s is NOT included in the search.
  5782.     A pointer to the first occurance of c in s is returned or NULL if c
  5783.     could not be found. c is converted to a char by stpchr before
  5784.     beginning the search.
  5785.  
  5786.     || NOTE: It is better to use the ANSI standard strchr and strrchr
  5787.     || functions.
  5788.  
  5789.     INPUTS
  5790.     char *s;        pointer to string to search
  5791.  
  5792.     int c;            character to search for
  5793.  
  5794.     RESULTS
  5795.     char *ptr;        pointer to the first occurance of the character c
  5796.                 in s, or NULL if c could not be found in s.
  5797.  
  5798.     SEE ALSO
  5799.     strchr, strrchr
  5800.  
  5801.     EXAMPLE
  5802.     #include <stdio.h>
  5803.     #include <string.h>
  5804.     #include <assert.h>
  5805.     main()
  5806.     {
  5807.        char *s = "this is a test";
  5808.        char *ptr;
  5809.        ptr = stpchr(s, 'i');
  5810.        assert(ptr == s + 2);
  5811.        puts(ptr);       /*  "is is a test"  */
  5812.        return(0);
  5813.     }
  5814.  
  5815. dice/stpcpy                                                      dice/stpcpy
  5816.  
  5817.     FUNCTION
  5818.     copy a string returning a pointer to the end of the destination
  5819.     (UNIX)
  5820.  
  5821.     SYNTAX
  5822.     #include <string.h>
  5823.     char *ptr = stpcpy(d, s);
  5824.     char *d;
  5825.     char *s;
  5826.  
  5827.     DESCRIPTION
  5828.     This function copies the NULL terminated string pointed to by s to
  5829.     the buffer d.  The NULL is copied.  A pointer to the NULL character
  5830.     at the end of the copied string in d is returned.
  5831.  
  5832.     || NOTE: stpcpy is non-standard.  While a stpcpy/stpcpy combination
  5833.     || is more efficient than a strcpy/strcat combination, strcpy and
  5834.     || strcat are standard functions and thus are guaranteed to exist in
  5835.     || all environments.
  5836.  
  5837.     INPUTS
  5838.     char *d;        pointer to beginning of destination buffer
  5839.  
  5840.     char *s;        pointer to beginning of source string
  5841.  
  5842.     RESULTS
  5843.     char *ptr;        pointer to end of data copied to destination
  5844.                 buffer
  5845.  
  5846.     SEE ALSO
  5847.     strcpy, strbpl
  5848.  
  5849.     EXAMPLE
  5850.     #include <stdio.h>
  5851.     #include <string.h>
  5852.     main()
  5853.     {
  5854.        char *buf1 = "Micky ";
  5855.        char *buf2 = "Moose";
  5856.        char dest[32];
  5857.        char *ptr;
  5858.        ptr = stpcpy(dest, buf1);
  5859.        stpcpy(ptr, buf2);
  5860.        puts(dest);       /* Micky Moose */
  5861.        return(0);
  5862.     }
  5863.  
  5864. dice/strbpl                                                      dice/strbpl
  5865.  
  5866.     FUNCTION
  5867.     unpack a string-array buffer into an array of pointers (DICE)
  5868.  
  5869.     SYNTAX
  5870.     #include <string.h>
  5871.     int num = strbpl(av, max, sary)
  5872.     char **av;
  5873.     int max;
  5874.     const char *sary;
  5875.  
  5876.     DESCRIPTION
  5877.     strbpl unpacks a string-array into an array of string pointers.  The
  5878.     string array is a series of NULL terminated strings strung together
  5879.     and terminated by a final NULL.  A pointer to each string is placed
  5880.     in the arary-of-pointers (av) with a final NULL entry assuming the
  5881.     number of strings does not exceed (max-1).
  5882.  
  5883.     INPUTS
  5884.     char **av;        pointer to a preallocated array of pointers
  5885.  
  5886.     int max;        the maximum number of entries in the above array
  5887.  
  5888.     char *sary;        pointer to a packed string.
  5889.  
  5890.     RESULTS
  5891.     int num;        number of pointers loaded into the av array not
  5892.                 including the final NULL.  If num == max then the
  5893.                 av array was not large enough to fit all the
  5894.                 strings or the final NULL.
  5895.  
  5896.     EXAMPLE
  5897.     #include <stdio.h>
  5898.     #include <string.h>
  5899.     #include <assert.h>
  5900.     main()
  5901.     {
  5902.        char *sary = "this\0is\0a\0test\0\0";
  5903.        char *av[16];
  5904.        int n;
  5905.        #define arysize(x) (sizeof(x)/sizeof((x)[0]))
  5906.        n = strbpl(av, arysize(av), sary);
  5907.        assert(n == 4);     /*  n == 4  */
  5908.        puts(av[0]);       /*  this    */
  5909.        puts(av[1]);       /*  is      */
  5910.        puts(av[2]);       /*  a       */
  5911.        puts(av[3]);       /*  test    */
  5912.        assert(av[4] == NULL); /*  av[4] == NULL   */
  5913.        return(0);
  5914.     }
  5915.  
  5916. dice/strcat                                                      dice/strcat
  5917.  
  5918.     FUNCTION
  5919.     concactenate a string to an existing string (ANSI)
  5920.  
  5921.     SYNTAX
  5922.     #include <string.h>
  5923.     char *d = strcat(d, s);
  5924.     char *d;
  5925.     const char *s;
  5926.  
  5927.     DESCRIPTION
  5928.     strcat scans the destination buffer for the NULL terminator and then
  5929.     appends the source string to the destination buffer (removing the
  5930.     NULL terminator and placing one at the end after the concactenation).
  5931.     A pointer to the beginning of the destination buffer is returned.
  5932.  
  5933.     INPUTS
  5934.     char *d;        pointer to destination buffer which already
  5935.                 contains a string (which could be just a \0).
  5936.  
  5937.     char *s;        pointer to the NULL terminated source string
  5938.  
  5939.     RESULTS
  5940.     char *d;        same as the first argument, a pointer to the
  5941.                 destination buffer.
  5942.  
  5943.     SEE ALSO
  5944.     strncpy, strcpy, strncat
  5945.  
  5946.     EXAMPLE
  5947.     #include <stdio.h>
  5948.     #include <string.h>
  5949.     main()
  5950.     {
  5951.        char dest[80];
  5952.        char *s1 = "I had an elegant proof also, ";
  5953.        char *s2 = "but the console window was too narrow...";
  5954.  
  5955.        strcpy(dest, s1);
  5956.        puts( strcat(dest, s2) ); /* returns its first arg */
  5957.     }
  5958.  
  5959. dice/strchr                                                      dice/strchr
  5960.  
  5961.     FUNCTION
  5962.     search for a character in a string (ANSI)
  5963.  
  5964.     SYNTAX
  5965.     #include <string.h>
  5966.     char *ptr = strchr(s, c)
  5967.     const char *s;
  5968.     int c;
  5969.  
  5970.     DESCRIPTION
  5971.     This searches for the character c within the string pointed to by s.
  5972.     The terminating NULL at the end of s is included in the search.  A
  5973.     pointer to the first occurance of c in s is returned or NULL if c
  5974.     could not be found. C is converted to a char by strchr before
  5975.     beginning the search.
  5976.  
  5977.     || NOTE: While strchr(s, 0); may be used to find the end of the
  5978.     || string this is slow compared to using the construction: char *ptr
  5979.     || = s + strlen(s);  /*  ptr = end of string s */.
  5980.  
  5981.     INPUTS
  5982.     char *s;        pointer to the string to search
  5983.  
  5984.     int c;            character to search for.
  5985.  
  5986.     RESULTS
  5987.     char *ptr;        pointer to the first occurance of character c in
  5988.                 s or NULL if c could not be found in s.
  5989.  
  5990.     SEE ALSO
  5991.     strrchr
  5992.  
  5993.     EXAMPLE
  5994.     #include <stdio.h>
  5995.     #include <string.h>
  5996.     #include <assert.h>
  5997.     main()
  5998.     {
  5999.        char *s = "We feel incredibly agile";
  6000.        char *ptr;
  6001.        ptr = strchr(s, 'i');
  6002.        assert(ptr == s + 2);
  6003.        puts(ptr);       /* "incredibly agile" */
  6004.        return(0);
  6005.     }
  6006.  
  6007. dice/strcmp                                                      dice/strcmp
  6008.  
  6009.     FUNCTION
  6010.     compare two strings (ANSI)
  6011.  
  6012.     SYNTAX
  6013.     #include <string.h>
  6014.     int r = strcmp(s1, s2);
  6015.     const char *s1;
  6016.     const char *s2;
  6017.  
  6018.     DESCRIPTION
  6019.     strcmp compares two strings, returning -1 if s1 < s2,  0 if s1 == s2
  6020.     , and 1 if  s1 > s2.
  6021.  
  6022.     || NOTE: strcmp converts the chars in the string to unsigned
  6023.     || quantities when comparing them.  However, for portability you
  6024.     || should not strcmp strings containing negative characters (bit 7
  6025.     || set) for anything other than checking the result against 0.    Use
  6026.     || the memcmp routine instead.
  6027.  
  6028.     INPUTS
  6029.     char *s1;        pointer to first string
  6030.  
  6031.     char *s2;        pointer to second string
  6032.  
  6033.     RESULTS
  6034.     int r;            -1, 0, or 1.
  6035.  
  6036.     SEE ALSO
  6037.     strncmp, stricmp
  6038.  
  6039.     EXAMPLE
  6040.     #include <stdio.h>
  6041.     #include <string.h>
  6042.     #include <assert.h>
  6043.     main()
  6044.     {
  6045.        char *s1 = "abca";
  6046.        char *s2 = "abcd";
  6047.        char *s3 = "abcx";
  6048.        char *s4 = "abcdx";
  6049.        char *s5 = "abc";
  6050.        char *x2 = "abcd";
  6051.        int r;
  6052.        r = strcmp(s2, x2);
  6053.        /*  string s2 same as string x2 */
  6054.        assert(r == 0);
  6055.        r = strcmp(s2, s1);
  6056.        /*  string s2 larger than string s1*/
  6057.        assert(r > 0);
  6058.        r = strcmp(s2, s3);
  6059.        assert(r < 0);
  6060.        r = strcmp(s2, s4);
  6061.        assert(r < 0);
  6062.        r = strcmp(s2, s5);
  6063.        assert(r > 0);
  6064.        return(0);
  6065.     }
  6066.  
  6067. dice/strcpy                                                      dice/strcpy
  6068.  
  6069.     FUNCTION
  6070.     copy a string returning a pointer to the beginning of the destination
  6071.     (ANSI)
  6072.  
  6073.     SYNTAX
  6074.     #include <string.h>
  6075.     char *ptr = strcpy(d, s);
  6076.     char *d;
  6077.     char *s;
  6078.  
  6079.     DESCRIPTION
  6080.     strcpy copies the NULL terminated string pointed to by s to the
  6081.     buffer d.  The NULL is copied.    The first argument is returned (a
  6082.     pointer to the buffer d).
  6083.  
  6084.     INPUTS
  6085.     char *d;        pointer to beginning of destination buffer
  6086.  
  6087.     char *s;        pointer to beginning of source string
  6088.  
  6089.     RESULTS
  6090.     char *ptr;        same as the destination buffer pointer (d).
  6091.  
  6092.     SEE ALSO
  6093.     stpcpy
  6094.  
  6095.     EXAMPLE
  6096.     #include <stdio.h>
  6097.     #include <string.h>
  6098.     #include <assert.h>
  6099.     /*
  6100.      *  Note that the stpcpy() example accomplishes the same
  6101.      *  thing and is more efficient, but also requires the
  6102.      *  use of a temporary pointer as well as cluttering the
  6103.      *  source and being non-standard.
  6104.      *  strcpy()/strcat() is more portable, though less
  6105.      *  efficient.
  6106.      */
  6107.      main()
  6108.     {
  6109.        char *buf1 = "saber";
  6110.        char *buf2 = "tooth";
  6111.        char dest[32];
  6112.  
  6113.        strcpy(dest, buf1);
  6114.        strcat(dest, buf2);
  6115.        puts(dest);       /* sabertooth */
  6116.        return(0);
  6117.     }
  6118.  
  6119. dice/strcspn                                                    dice/strcspn
  6120.  
  6121.     FUNCTION
  6122.     scan a string until a character is found that matches any character
  6123.     in a second string (ANSI)
  6124.  
  6125.     SYNTAX
  6126.     #include <string.h>
  6127.     int len = strcspn(s, toks)
  6128.     const char *s;
  6129.     const char *toks;
  6130.  
  6131.     DESCRIPTION
  6132.     With strcspn, the string s is scanned until a character is found that
  6133.     matches any character in the string toks.  The number of characters
  6134.     skipped is returned.  If no character in s matches any character in
  6135.      toks then the length of the string s is returned.
  6136.     strcspn is normally used to search for whitespace within a string.
  6137.     Note that in many cases strpbrk is more useful than strcspn.
  6138.  
  6139.     INPUTS
  6140.     char *s;        pointer to string to scan
  6141.  
  6142.     char *toks;        pointer to string containing characters to
  6143.                 compare against
  6144.  
  6145.     RESULTS
  6146.     int len;        # of characters skipped in s before a match was
  6147.                 found.
  6148.  
  6149.     SEE ALSO
  6150.     strpbrk, strspn
  6151.  
  6152.     EXAMPLE
  6153.     #include <stdio.h>
  6154.     #include <string.h>
  6155.     #include <assert.h>
  6156.     main()
  6157.     {
  6158.        int len;
  6159.        len = strcspn("hello this is a test", " \tabcd");
  6160.        assert(len == 5);  /*  stopped at the first space  */
  6161.        len = strcspn("hello this is a test", " abl");
  6162.        assert(len == 2);  /*  stopped at the first 'l'    */
  6163.        len = strcspn("hello", "abcd");
  6164.        assert(len == 5);  /*  stopped at end of string 1  */
  6165.        return(0);
  6166.     }
  6167.  
  6168. dice/strdup                                                      dice/strdup
  6169.  
  6170.     FUNCTION
  6171.     duplicate a string using malloc (DICE)
  6172.  
  6173.     SYNTAX
  6174.     #include <string.h>
  6175.     char *s2 = strdup(s1);
  6176.     const char *s1;
  6177.  
  6178.     DESCRIPTION
  6179.     strdup allocates enough space to hold s1 including the terminating
  6180.     NULL and then copies s1 into this space, returning a pointer to the
  6181.     new string.  NULL is returned if space could not be allocated due to
  6182.     low memory conditions.    Note free may be used to free the returned
  6183.     string.  The amount allocated is (strlen(s1) + 1).
  6184.  
  6185.     || NOTE: This is a non-standard function and may not exist in other C
  6186.     || environments.
  6187.  
  6188.     INPUTS
  6189.     char *s1;        pointer to the string to duplicate
  6190.  
  6191.     RESULTS
  6192.     char *s2;        pointer to malloc'd space containing a duplicate
  6193.                 of the string s1 or NULL if space could not be
  6194.                 malloc'd.
  6195.  
  6196.     SEE ALSO
  6197.     malloc, free, strcpy, strlen
  6198.  
  6199.     EXAMPLE
  6200.     #include <stdio.h>
  6201.     #include <string.h>
  6202.     #include <assert.h>
  6203.     /*
  6204.      *  Modifying string constants (quoted strings) may
  6205.      *  not be entirely portable.  Normally one does not
  6206.      *  use strdup() to accomplish the following function
  6207.      *  but instead declares a char array statically
  6208.      *  initialized with the string, such as:
  6209.      *  char FuBar[] = { "This is a test" };
  6210.      *  Which can be modified in a portable fashion without
  6211.      *  having to duplicate the string.
  6212.      */
  6213.     main()
  6214.     {
  6215.        char *s1 = "this is a test";
  6216.        char *s2;
  6217.        s2 = strdup(s1);
  6218.        s2[0] = 'x';
  6219.        puts(s2);   /*  this is a test  */
  6220.        free(s2);
  6221.        s2 = strdup(s1);
  6222.        s2[1] = '0';
  6223.        puts(s2);   /*  this is a test  */
  6224.        free(s2);
  6225.        return(0);
  6226.     }
  6227.  
  6228. dice/strerror                                                  dice/strerror
  6229.  
  6230.     FUNCTION
  6231.     return error string associated with error code (ANSI)
  6232.  
  6233.     SYNTAX
  6234.     #include <string.h>
  6235.     const char *str = strerror(error); int error;
  6236.  
  6237.     DESCRIPTION
  6238.     strerror returns a read-only string associated with the specified
  6239.     error, usually taken from errno after some c.lib call fails.  An
  6240.     unknown error will result in the string "unknown error."
  6241.  
  6242.     INPUTS
  6243.     int error;        error code
  6244.  
  6245.     RESULTS
  6246.     char *str;        error string
  6247.  
  6248.     SEE ALSO
  6249.     perror
  6250.  
  6251.     EXAMPLE
  6252.     #include <stdio.h>
  6253.     #include <string.h>
  6254.     #include <errno.h>
  6255.     #include <assert.h>
  6256.     main()
  6257.     {
  6258.        FILE *fi;
  6259.        fi = fopen("ThisFileDoesNotExist", "r");
  6260.        assert(fi == NULL);
  6261.        puts(strerror(errno));
  6262.        return(0);
  6263.     }
  6264.  
  6265. dice/strftime                                                  dice/strftime
  6266.  
  6267.     FUNCTION
  6268.     convert broken down time into a string according to a format (ANSI)
  6269.  
  6270.     SYNTAX
  6271.     #include <time.h>
  6272.     size_t len = strftime(buf, max, fmt, tm)
  6273.     char *buf; size_t max;
  6274.     const char *fmt;
  6275.     const struct tm *tm;
  6276.  
  6277.     DESCRIPTION
  6278.     strftime formats a broken down time into a buffer according to a
  6279.     format string fmt.  The fmt string looks like a the format for a
  6280.     printf except with different control definitions:
  6281.  
  6282.     %%  A literal '%' character.
  6283.  
  6284.     %a  The locale's abbreviated name for the day of week.
  6285.  
  6286.     %A  The locale's full name for the  day of week.
  6287.  
  6288.     %b  The locale's abbr. name for the month.
  6289.  
  6290.     %B  The locale's full name for the month.
  6291.  
  6292.     %c  The locale's default representation for the date & time (ctime).
  6293.  
  6294.     %d  The day of the month 01-31.
  6295.  
  6296.     %H  The hour 00-23 (24 hour time).
  6297.  
  6298.     %I  The hour 01-12 (12 hour time).
  6299.  
  6300.     %j  The day in the year 001-366.
  6301.  
  6302.     %m  The month 01-12.
  6303.  
  6304.     %M  The minute 00-59.
  6305.  
  6306.     %p  Indication of morning or afternoon.  In the US: "AM" or "PM".
  6307.  
  6308.     %S  The second 00-59.
  6309.  
  6310.     %U  The week of the year 00-53, Sunday is the first day in a week.
  6311.  
  6312.     %w  The day of the week 0-6, Sunday=0 (standard).
  6313.  
  6314.     %W  The day of the week 0-6, monday=0.
  6315.  
  6316.     %x  The locale's default representation for the date only.
  6317.  
  6318.     %X  The locale's default representation for the time only.
  6319.  
  6320.     %y  The year mod 100 (00-99).
  6321.  
  6322.     %Y  The full year (e.g. 1990).
  6323.  
  6324.     %Z  The name of the locale's time zone, nothing if unknown.
  6325.  
  6326.         ## WARNING: There must be at least max + 1 bytes in buf or you
  6327.         ## might unexpectedly overwrite memory.
  6328.  
  6329.     INPUTS
  6330.     char *buf;        buffer to write formatted string into
  6331.  
  6332.     size_t max;        maximum size of buffer - 1
  6333.  
  6334.     char *fmt;        format string
  6335.  
  6336.     struct tm *tm;        broken down time
  6337.  
  6338.     RESULTS
  6339.     size_t len;        length of formatted string in buffer or 0 if the
  6340.                 maximum was exceeded.
  6341.  
  6342.     SEE ALSO
  6343.     time, localtime, asctime, ctime, clock
  6344.  
  6345.     EXAMPLE
  6346.     #include <stdio.h>
  6347.     #include <time.h>
  6348.  
  6349.     main()
  6350.     {
  6351.        time_t t = time(NULL);
  6352.        struct tm *tp = localtime(&t);
  6353.        char buf[256];
  6354.        strftime(buf, sizeof(buf) - 1,
  6355.             "Now is %A %d %B %Y %X",  tp);
  6356.        puts(buf);
  6357.        return(0);
  6358.     }
  6359.  
  6360. dice/stricmp                                                    dice/stricmp
  6361.  
  6362.     FUNCTION
  6363.     compare two strings, case insensitive (UNIX)
  6364.  
  6365.     SYNTAX
  6366.     #include <string.h>
  6367.     int r = stricmp(s1, s2);
  6368.     const char *s1;
  6369.     const char *s2;
  6370.  
  6371.     DESCRIPTION
  6372.     stricmp compares two strings, returning: -1 if    s1 < s2, 0 if  s1 ==
  6373.     s2, or 1 if  s1 > s2. differs from strcmp in that case is ignored for
  6374.     alphabetic characters, i.e. a == A.
  6375.  
  6376.     || NOTE: NOTE= stricmp converts the chars in the string to unsigned
  6377.     || quantities when comparing them.  However, for portability you
  6378.     || should not stricmp strings containing negative characters (bit 7
  6379.     || set) for anything other than checking the result against 0.    Use
  6380.     || the memcmp routine instead.
  6381.  
  6382.     INPUTS
  6383.     char *s1;        pointer to first string
  6384.  
  6385.     char *s2;        pointer to second string
  6386.  
  6387.     RESULTS
  6388.     int r;            -1, 0, or 1.
  6389.  
  6390.     SEE ALSO
  6391.     strcmp, strncmp
  6392.  
  6393.     EXAMPLE
  6394.     #include <stdio.h>
  6395.     #include <string.h>
  6396.     #include <assert.h>
  6397.     main()
  6398.     {
  6399.        char *s1 = "abCa";
  6400.        char *s2 = "aBcD";
  6401.        char *s3 = "aBCX";
  6402.        char *s4 = "ABCdx";
  6403.        char *s5 = "Abc";
  6404.        char *x2 = "ABCD";
  6405.        int r;
  6406.        r = stricmp(s2, x2); /* string s2 same as x2 */
  6407.        assert(r == 0);
  6408.        r = stricmp(s2, s1); /* string s2 larger than s1 */
  6409.        assert(r > 0);
  6410.        r = stricmp(s2, s3);
  6411.        assert(r < 0);
  6412.        r = stricmp(s2, s4);
  6413.        assert(r < 0);
  6414.        r = stricmp(s2, s5);
  6415.        assert(r > 0);
  6416.        return(0);
  6417.     }
  6418.  
  6419. dice/strins                                                      dice/strins
  6420.  
  6421.     FUNCTION
  6422.     insert one string within another (DICE)
  6423.  
  6424.     SYNTAX
  6425.     #include <string.h>
  6426.     void strins(d, s);
  6427.     char *d;
  6428.     const char *s;
  6429.  
  6430.     DESCRIPTION
  6431.     strins inserts string s into d by shifting the string in d over
  6432.     strlen(s) spaces and then copying s into the newly made hold (except
  6433.     for the NULL, of course).  This result is s inserted into d.
  6434.  
  6435.     || NOTE: There must be enough room in d to insert s; if d is an array
  6436.     || of 32 chars and contains a string of 8 chars you can insert
  6437.     || another string of, say, 10 chars, but not of 30 chars.  strins is
  6438.     || not an ANSI standard function.
  6439.  
  6440.     INPUTS
  6441.     char *d;        destination to insert in front of
  6442.  
  6443.     char *s;        source string to insert
  6444.  
  6445.     SEE ALSO
  6446.     strcpy, strcat, strlen
  6447.  
  6448.     EXAMPLE
  6449.     #include <stdio.h>
  6450.     #include <string.h>
  6451.     main()
  6452.     {
  6453.        char buf[32];
  6454.        strcpy(buf, "This is a test");
  6455.        strins(buf + 5, "<gak!> ");
  6456.        puts(buf);
  6457.        /*  This <gak!> is a test  */
  6458.        return(0);
  6459.     }
  6460.  
  6461. dice/strlen                                                      dice/strlen
  6462.  
  6463.     FUNCTION
  6464.     returns length of a string (ANSI)
  6465.  
  6466.     SYNTAX
  6467.     #include <string.h>
  6468.     int len = strlen(s);
  6469.     const char *s;
  6470.  
  6471.     DESCRIPTION
  6472.     When this function is used, the length of the requested string is
  6473.     returned. The string is scanned until a NULL terminator is found and
  6474.     the number of characters (not including the NULL) is returned.
  6475.  
  6476.     :: Beginner's Note: The length returned by strlen does not include
  6477.     :: the NULL.  It is common error to forget this fact and reserve one
  6478.     :: too few bytes of storage!
  6479.  
  6480.     INPUTS
  6481.     char *s;        string to obtain length of
  6482.  
  6483.     RESULTS
  6484.     int len;        length of string
  6485.  
  6486.     SEE ALSO
  6487.     strcpy, strcat
  6488.  
  6489.     EXAMPLE
  6490.     #include <stdio.h>
  6491.     #include <string.h>
  6492.     main(int ac, char **av)
  6493.     {
  6494.         if( ac  1 ) {
  6495.         printf("%s is %d byte(s) long\n",
  6496.                av[1], strlen( av[1] ) );
  6497.         } else
  6498.         printf("Gimme, gimme, gimme!!\n");
  6499.     }
  6500.  
  6501. dice/strncat                                                    dice/strncat
  6502.  
  6503.     FUNCTION
  6504.     concactenate a string to an existing string up to a maximum number of
  6505.     characters (ANSI)
  6506.  
  6507.     SYNTAX
  6508.     #include <string.h>
  6509.     char *d = strncat(d, s, n);
  6510.     char *d;
  6511.     const char *s;
  6512.     int n;
  6513.  
  6514.     DESCRIPTION
  6515.     strncat scans the destination buffer for the NULL terminator and then
  6516.     appends the source string to the destination buffer (removing the
  6517.     NULL terminator and placing one at the end after the concactenation).
  6518.     However, only up to n characters is concactenated including the NULL.
  6519.     If the source string is exactly n characters long no NULL will be
  6520.     appended.  If the source string is longer than n characters then only
  6521.     the first n characters of the source string will be appended (and no
  6522.     NULL will be).    A pointer to the beginning of the destination buffer
  6523.     is returned.
  6524.  
  6525.     INPUTS
  6526.     char *d;        pointer to destination buffer which already
  6527.                 contains a string (which could be just a \0).
  6528.  
  6529.     char *s;        pointer to the NULL terminated source string
  6530.  
  6531.     int n;            maximum number of characters to concactenate
  6532.  
  6533.     RESULTS
  6534.     char *d;        same as the first argument, a pointer to the
  6535.                 destination buffer.
  6536.  
  6537.     SEE ALSO
  6538.     strncpy, strcpy, strcat
  6539.  
  6540.     EXAMPLE
  6541.     #include <stdio.h>
  6542.     #include <string.h>
  6543.     main()
  6544.     {
  6545.        char d[32];           char *p;
  6546.        char *s1 = "grid";  char *s2 = "lock";
  6547.  
  6548.        strcpy( d, s1 );
  6549.        p = strncat( d, s2, 10 );
  6550.        puts( d );  /* gridlock */
  6551.  
  6552.        strcpy( d,I s1 );
  6553.        p = strncat( d, s2, 3 );
  6554.        puts( d );  /* gridloc  */
  6555.  
  6556.        return(0);
  6557.     }
  6558.  
  6559. dice/strncmp                                                    dice/strncmp
  6560.  
  6561.     FUNCTION
  6562.     compare two strings up to a maximum number of characters (ANSI)
  6563.  
  6564.     SYNTAX
  6565.     #include <string.h>
  6566.     int r = strncmp(s1, s2, n);
  6567.     const char *s1;
  6568.     const char *s2;
  6569.     int n;
  6570.  
  6571.     DESCRIPTION
  6572.     strncmp compares two strings, returning:  -1 if s1 < s2,  0 if s1 ==
  6573.     s2, or 1 if  s1 > s2.    strncmp works like strcmp but only up to n
  6574.     characters will be compared.  If all characters compare when n is
  6575.     reached 0 is returned indicating that the strings matched. Fewer
  6576.     characters might be compared if either string terminates (w/ a NULL)
  6577.     before the maximum is reached or a compare fails (scan is stopped and
  6578.     -1 or 1 is returned immediately).
  6579.  
  6580.     || NOTE: strncmp converts the chars in the string to unsigned
  6581.     || quantities when comparing them.  However, for portability you
  6582.     || should not strncmp strings containing negative characters (bit 7
  6583.     || set) for anything other than checking the result against 0.    Use
  6584.     || the memcmp routine instead.
  6585.  
  6586.     INPUTS
  6587.     char *s1;        pointer to first string
  6588.  
  6589.     char * s2;        pointer to second string
  6590.  
  6591.     int n;            maximum number of characters to compare
  6592.  
  6593.     RESULTS
  6594.     int r;            -1, 0, or 1.
  6595.  
  6596.     SEE ALSO
  6597.     stricmp
  6598.  
  6599.     EXAMPLE
  6600.     #include <stdio.h>
  6601.     #include <string.h>
  6602.     #include <assert.h>
  6603.     main()
  6604.     {
  6605.        char *s1 = "abcaq";
  6606.        char *s2 = "abcdr";
  6607.        char  *s3 = "abcxs";
  6608.        char *s4 = "abcdxx";
  6609.        char *s5 = "abc";
  6610.        char *x2 = "abcdt";
  6611.        int r;   r = strncmp(s2, x2, 4);
  6612.        assert(r == 0);
  6613.        r = strncmp(s2, s1, 4);
  6614.        assert(r > 0);
  6615.        r = strncmp(s2, s3, 4);
  6616.        assert(r < 0);
  6617.        r = strncmp(s2, s4, 8);
  6618.        assert(r < 0);
  6619.        r = strncmp(s2, s5, 4);
  6620.        assert(r > 0);
  6621.        return(0);
  6622.     }
  6623.  
  6624. dice/strncpy                                                    dice/strncpy
  6625.  
  6626.     FUNCTION
  6627.     copy a string returning a pointer to the beginning of the destination
  6628.     until NULL or the specified number of characters is reached (ANSI)
  6629.  
  6630.     SYNTAX
  6631.     #include <string.h>
  6632.     char *ptr = strncpy(d, s, n);
  6633.     char *d;
  6634.     const char *s;
  6635.     int n;
  6636.  
  6637.     DESCRIPTION
  6638.     strncpy copies the NULL terminated string pointed to by s to the
  6639.     buffer d. The NULL is normally copied.    The first argument is
  6640.     returned (a pointer to the buffer d). The copy will also be
  6641.     terminated if the specified maximum is reached, in which case the
  6642.     NULL is NOT copied (which makes the function useless - but hey, it
  6643.     does make it standard).
  6644.  
  6645.     INPUTS
  6646.     char *d;        pointer to beginning of destination buffer
  6647.  
  6648.     char *s;        pointer to beginning of source string
  6649.  
  6650.     int len;        maximum number of characters to copy
  6651.  
  6652.     RESULTS
  6653.     char *ptr;        same as the destination buffer pointer (d).
  6654.  
  6655.     SEE ALSO
  6656.     stpcpy, strcpy
  6657.  
  6658.     EXAMPLE
  6659.     #include <stdio.h>
  6660.     #include <string.h>
  6661.     #include <assert.h>
  6662.     main()
  6663.     {
  6664.        char *buf1 = "hello";
  6665.        char *buf2 = "123";
  6666.        char dest[32];
  6667.  
  6668.        strncpy(dest, buf1, 8);
  6669.        strcat(dest, buf2);
  6670.        puts(dest);           /* hello123 */
  6671.  
  6672.        dest[2] = 23;
  6673.        strncpy(dest, buf1, 2);
  6674.        assert(dest[2] == 23);  /* copy just two */
  6675.        dest[2] = 0;  /* we have to add the NULL */
  6676.        puts(dest);           /* he */
  6677.  
  6678.        strcat(dest, buf2);       /* he123   */
  6679.        puts(dest);
  6680.        return(0);
  6681.     }
  6682.  
  6683. dice/strnicmp                                                  dice/strnicmp
  6684.  
  6685.     FUNCTION
  6686.     compare two strings up to a maximum number of characters, case
  6687.     insensitive (UNIX)
  6688.  
  6689.     SYNTAX
  6690.     #include <string.h>
  6691.     int r = strnicmp(s1, s2, n);
  6692.     const char *s1;
  6693.     const char *s2;
  6694.     int n;
  6695.  
  6696.     DESCRIPTION
  6697.     strnicmp compares two strings, returning:  -1,    s1 < s2;  0, s1 ==
  6698.     s2;   1,  s1 > s2.  strnicmp differs from strcmp in that case is
  6699.     ignored for alphabetic characters (i.e., a == A) and only up to n
  6700.     characters are compared. Refer to stricmp and strncmp for other
  6701.     examples.
  6702.  
  6703.     || NOTE: strnicmp converts the chars in the string to unsigned
  6704.     || quantities when comparing them.  However, for portability you
  6705.     || should not strnicmp strings containing negative characters (bit 7
  6706.     || set) for anything other than checking the result against 0.    Use
  6707.     || the memcmp routine instead.
  6708.  
  6709.     INPUTS
  6710.     char *s1;        pointer to first string
  6711.  
  6712.     char *s2;        pointer to second string
  6713.  
  6714.     int n;            maximum # of characters to compare
  6715.  
  6716.     RESULTS
  6717.     int r;            -1, 0, or 1.
  6718.  
  6719.     SEE ALSO
  6720.     strcmp, strncmp, stricmp
  6721.  
  6722.     EXAMPLE
  6723.     #include <stdio.h>
  6724.     #include <string.h>
  6725.     #include <assert.h>
  6726.     main()
  6727.     {
  6728.        char *s1 = "aBcAQ";
  6729.        char *s2 = "abCDR";
  6730.        char *s3 = "ABcXs";
  6731.        char *s4 = "aBCDxX";
  6732.        char *s5 = "aBC";
  6733.        char *x2 = "AbCDt";
  6734.        int r;
  6735.        r = strnicmp(s2, x2, 4);
  6736.        assert(r == 0);
  6737.        r = strnicmp(s2, s1, 4);
  6738.        assert(r > 0);
  6739.        r = strnicmp(s2, s3, 4);
  6740.        assert(r < 0);
  6741.        r = strnicmp(s2, s4, 8);
  6742.        assert(r < 0);
  6743.        r = strnicmp(s2, s5, 4);
  6744.        assert(r > 0);
  6745.        return(0);
  6746.     }
  6747.  
  6748. dice/strpbrk                                                    dice/strpbrk
  6749.  
  6750.     FUNCTION
  6751.     search for specific characters in a string (ANSI)
  6752.  
  6753.     SYNTAX
  6754.     #include <string.h>
  6755.     char *ptr = strpbrk(s, toks)
  6756.     const char *s;
  6757.     char *toks;
  6758.  
  6759.     DESCRIPTION
  6760.     strpbrk searches the string s for any character in the string toks.
  6761.     For example, when searching for whitespace in s, toks would contain
  6762.     the space and tab character.  If no character in s matches any
  6763.     character in toks then NULL is returned.
  6764.  
  6765.     INPUTS
  6766.     char *s;        pointer to string to scan
  6767.  
  6768.     char *toks;        pointer to string containing tokens to scan for
  6769.  
  6770.     RESULTS
  6771.     char *ptr;        pointer to point in s where the character matches
  6772.                 any character in toks, or NULL if s was
  6773.                 exhausted.
  6774.  
  6775.     SEE ALSO
  6776.     strtok
  6777.  
  6778.     EXAMPLE
  6779.     #include <stdio.h>
  6780.     #include <string.h>
  6781.     #include <assert.h>
  6782.     main()
  6783.     {
  6784.        char *s = "This  \tis a test";
  6785.        char *ptr;
  6786.        ptr = strpbrk(s, " \t");
  6787.        assert(ptr == s + 4);
  6788.        ptr = strpbrk(ptr + 1, " \t");
  6789.        assert(ptr == s + 5);
  6790.        ptr = strpbrk(ptr + 1, " \t");
  6791.        assert(ptr == s + 6);
  6792.        ptr = strpbrk(ptr + 1, " \t");
  6793.        assert(ptr == s + 9);
  6794.        ptr = strpbrk(ptr + 1, "xyz"); /* doesn't find 'm */
  6795.        assert(ptr == NULL);
  6796.        return(0);
  6797.     }
  6798.  
  6799. dice/strrchr                                                    dice/strrchr
  6800.  
  6801.     FUNCTION
  6802.     search for a character in a string, scan backwards (ANSI)
  6803.  
  6804.     SYNTAX
  6805.     #include <string.h>
  6806.     char *ptr = strrchr(s, c)
  6807.     const char *s;
  6808.     int c;
  6809.  
  6810.     DESCRIPTION
  6811.     strrchr searches for the character c within the string pointed to by
  6812.     s. The terminating NULL at the end of s is included in the search.
  6813.     The string is searched backwards. A pointer to the last occurrance of
  6814.     c in s is returned or NULL if c could not be found.  C is converted
  6815.     to an 8 bit quantity by strrchr.
  6816.  
  6817.     || NOTE: The ANSI spec does not say anything about including the NULL
  6818.     || character in the search for strrchr and some implementation may
  6819.     || thus not implement this properly.
  6820.  
  6821.     INPUTS
  6822.     char *s;        pointer to the string to search
  6823.  
  6824.     int c;            character to search for
  6825.  
  6826.     RESULTS
  6827.     char *ptr;        pointer to the last occurance of character c in s
  6828.                 or NULL if c could not be found in s.
  6829.  
  6830.     SEE ALSO
  6831.     strchr
  6832.  
  6833.     EXAMPLE
  6834.     #include <stdio.h>
  6835.     #include <string.h>
  6836.     #include <assert.h>
  6837.     main()
  6838.     {
  6839.        char *s = "this is a test";
  6840.        char *ptr;
  6841.        ptr = strrchr(s, 'i');
  6842.        assert(ptr == s + 5);
  6843.        puts(ptr);       /*  "is a test"  */
  6844.        ptr = strrchr(s, 'x');
  6845.        assert(ptr == NULL);
  6846.        return(0);
  6847.     }
  6848.  
  6849. dice/strspn                                                      dice/strspn
  6850.  
  6851.     FUNCTION
  6852.     scan a string until a character is found that does not match some
  6853.     character in a second string (ANSI)
  6854.  
  6855.     SYNTAX
  6856.     #include <string.h>
  6857.     int len = strspn(s, toks)
  6858.     const char *s;
  6859.     const char *toks;
  6860.  
  6861.     DESCRIPTION
  6862.     The string s is scanned until a character is found that does not
  6863.     match any character in the string toks.  The number of characters
  6864.     skipped is returned.  If every character in s matches some character
  6865.     in toks then the length of the string s is returned.
  6866.  
  6867.     strspn is normally used to skip whitespace within a string.
  6868.  
  6869.     INPUTS
  6870.     char *s;        pointer to string to scan
  6871.  
  6872.     char *toks;        pointer to string containing characters to
  6873.                 compare against
  6874.  
  6875.     RESULTS
  6876.     int len;        # of characters skipped in s before a match could
  6877.                 not be found
  6878.  
  6879.     SEE ALSO
  6880.     strpbrk, strcspn
  6881.  
  6882.     EXAMPLE
  6883.     #include <stdio.h>
  6884.     #include <string.h>
  6885.     #include <assert.h>
  6886.  
  6887.     main()
  6888.     {
  6889.        int len;
  6890.        len = strspn("  \t \t\t abcde test", " \t ");
  6891.        assert(len == 7);  /*  stopped at the 'a'  */
  6892.        len = strspn("abcd efg", " ");
  6893.        assert(len == 0);  /*  stopped at the 'a'  */
  6894.        len = strspn("   \t\t ", " \t");
  6895.        assert(len == 6);  /*  all match, len=strlen(str); */
  6896.        return(0);
  6897.     }
  6898.  
  6899. dice/strstr                                                      dice/strstr
  6900.  
  6901.     FUNCTION
  6902.     find sub-string within another string (ANSI)
  6903.  
  6904.     SYNTAX
  6905.     #include <string.h>
  6906.     char *ptr = strstr(s, sub);
  6907.     const char *s;
  6908.     const char *sub;
  6909.  
  6910.     DESCRIPTION
  6911.     The string s is scanned until the sub-string sub matches the string
  6912.     beginning at the current scan point, and a pointer to the sub-string
  6913.     within s is returned.  If the sub-string could not be found NULL is
  6914.     returned.
  6915.  
  6916.     INPUTS
  6917.     char *s;        pointer to string to scan
  6918.  
  6919.     char *toks;        pointer to string containing characters to
  6920.                 compare against
  6921.  
  6922.     RESULTS
  6923.     char *ptr;        point in s where sub string was found or NULL if
  6924.                 sub string could not be found.
  6925.  
  6926.     SEE ALSO
  6927.     strpbrk, strcspn
  6928.  
  6929.     EXAMPLE
  6930.     #include <stdio.h>
  6931.     #include <string.h>
  6932.     #include <assert.h>
  6933.  
  6934.     main()
  6935.     {
  6936.        char *s = "abcdefghijklmnopqrstuvwxyz";
  6937.        char *ptr;
  6938.        ptr = strstr(s, "klm");
  6939.        assert(ptr == s + 10);
  6940.        puts(ptr);  /*  klmnopqrstuvwxyz    */
  6941.        return(0);
  6942.     }
  6943.  
  6944. dice/strtod                                                      dice/strtod
  6945.  
  6946.     FUNCTION
  6947.     convert string to fp double (ANSI)
  6948.  
  6949.     LIBRARY
  6950.     m.lib
  6951.  
  6952.     SYNTAX
  6953.     #include <string.h>
  6954.     double d = strtod(s, &tp);
  6955.     const char *s;
  6956.     char *tp;
  6957.  
  6958.     DESCRIPTION
  6959.     strtod converts a string to a floating point double.  Initial
  6960.     whitespace is skipped.    The format of the fp number in the string is
  6961.     then:
  6962.  
  6963.     {+/-/<nothing>}ddddd[.ddddd][E{+/-/<nothing>}dddd]
  6964.  
  6965.     Example fp strings: " +1.234E-3", "-1234", "6.5676E4", "214.2345"
  6966.  
  6967.     INPUTS
  6968.     char *s;        pointer to string containing fp number
  6969.  
  6970.     char **tp;        pointer to pointer, the pointer is modified to
  6971.                 point to the end of the scanned fp number.
  6972.  
  6973.     RESULTS
  6974.     double d;        resulting double
  6975.  
  6976.     EXAMPLE
  6977.     /*
  6978.      *  compile -lm to include math library
  6979.      */
  6980.     #include <stdio.h>
  6981.     #include <string.h>
  6982.  
  6983.     main()
  6984.     {
  6985.        double d;
  6986.        char *tp;
  6987.        d = strtod("1.2134 3.45E2", &tp);
  6988.        printf("1.2134 = %lf\n", d);    /*  1.213400    */
  6989.        d = strtod(tp, &tp);
  6990.        printf("3.45E2 = %lf\n", d);    /*  345.000000  */
  6991.        return(0);
  6992.     }
  6993.  
  6994. dice/strtok                                                      dice/strtok
  6995.  
  6996.     FUNCTION
  6997.     break up a string into arguments (ANSI)
  6998.  
  6999.     SYNTAX
  7000.     #include <string.h>
  7001.     char *arg = strtok(s, toks)
  7002.     char *s;
  7003.     const char *toks;
  7004.  
  7005.     DESCRIPTION
  7006.     strtok breaks up a string into arguments.  It determines the break
  7007.     point from the toks string which contains a set of whitespace
  7008.     characters (usually \t to mean space and tab).    The first call to
  7009.     strtok should specify the string s and toks. Initial whitespace is
  7010.     skipped and the string is then scanned until the end of the first
  7011.     argument is found.  The string is then modified: a NULL is placed at
  7012.     the end of the first argument and a pointer to the beginning of the
  7013.     first argument is returned.
  7014.  
  7015.     Further calls to strtok should pass a NULL for the string s, which
  7016.     tells strtok to continue scanning the original string (whose pointer
  7017.     was stored in a static char * within strtok). strtok returns
  7018.     arguments until the string is exhausted, in which case it returns
  7019.     NULL.  The initial call to strtok can return NULL if the passed
  7020.     string s contains nothing but whitespace (as specified by toks).  You
  7021.     can change the toks string at any time (i.e. pass a different toks
  7022.     string to strtok).
  7023.  
  7024.     ## WARNING: strtok modifies the source string and returns pointers
  7025.     ## into it.
  7026.  
  7027.     INPUTS
  7028.     char *s;        pointer to string to parse
  7029.  
  7030.     char *toks;        pointer to string containing whitespace
  7031.                 characters (argument delimiters)
  7032.  
  7033.     RESULTS
  7034.     char *arg;        pointer into s to next argument that is NULL
  7035.                 terminated (s is modified).
  7036.  
  7037.     SEE ALSO
  7038.     strspn, strcspn
  7039.  
  7040.     EXAMPLE
  7041.     #include <stdio.h>
  7042.     #include <string.h>
  7043.  
  7044.     main()
  7045.     {
  7046.        char buf[32];
  7047.        char *arg;
  7048.        const char *ws = " \t";
  7049.        /*
  7050.         *  'This' 'is' 'a' 'test!'
  7051.         */
  7052.        strcpy(buf, "  This  is \t \t a test!");
  7053.        for (arg = strtok(buf, ws);
  7054.         arg; arg = strtok(NULL, ws))
  7055.        {
  7056.           printf("arg = '%s'\n", arg);
  7057.        }
  7058.        return(0);
  7059.     }
  7060.  
  7061. dice/strtol,strtoul                                      dice/strtol,strtoul
  7062.  
  7063.     FUNCTION
  7064.     convert string to integer (ANSI)
  7065.  
  7066.     SYNTAX
  7067.     #include <string.h>
  7068.     long v = strtol(str, &tail, base);
  7069.     unsigned long v = strtoul(str, &tail, base);
  7070.     const char *str;
  7071.     char *tail;
  7072.     int base;
  7073.  
  7074.     DESCRIPTION
  7075.     strtol converts a string into an integer using the specified base
  7076.     0-36.  If a non-zero base is specified conversion is done using that
  7077.     base (hex numbers may still be preceeded by '0x' or '0X').  If 0 is
  7078.     specified for the base then the base is determined from the first one
  7079.     or two characters of the number portion of the string:
  7080.  
  7081.         0     octal
  7082.         1-9     decimal
  7083.     0x     hex (0x or 0X)
  7084.  
  7085.     For bases larger than 10, alphabetic characters are used to represent
  7086.     digits. Either lower case or upper case letters may be used.  strtol
  7087.     stores a pointer to the remainder of the string after the conversion.
  7088.     strtol ignores any whitespace at the beginning of the string and also
  7089.     handles an optional negative sign (which may precede the numerical
  7090.      portion of the string).
  7091.     If successful strtol returns the converted value as a long, 0 if it
  7092.     was unable to convert anything, and an undefined result if the
  7093.     converted value was out of range.
  7094.  
  7095.     || NOTE: strtol supersedes atoi and atol.
  7096.  
  7097.     INPUTS
  7098.     char *str;        pointer to string to convert
  7099.  
  7100.     char **tail;        *tail modified to point to just after last
  7101.                 character converted
  7102.  
  7103.     int base;        base of conversion or 0 for autoselect
  7104.  
  7105.     RESULTS
  7106.     long v;         converted result, an integer, or 0 if no
  7107.                 conversion could be done.
  7108.  
  7109.     SEE ALSO
  7110.     atoi, atol, strtod
  7111.  
  7112.     EXAMPLE
  7113.     #include <stdio.h>
  7114.     #include <string.h>
  7115.  
  7116.     main(int ac, char**av)
  7117.     {
  7118.        long v;
  7119.        char *tail;
  7120.        if (ac != 3)
  7121.        {
  7122.           puts("testprg <string> <base>");
  7123.           puts("testprg 0123abc 0");
  7124.           puts("testprg 0x1000 0");
  7125.           puts("testprg 0123abc 16");
  7126.           exit(1);
  7127.        }
  7128.        v = strtol(av[1], &tail, atoi(av[2]));
  7129.        printf("v = %d, tail = %s\n", v, tail);
  7130.        return(0);
  7131.     }
  7132.  
  7133. dice/system                                                      dice/system
  7134.  
  7135.     FUNCTION
  7136.     call system shell with a command line (ANSI)
  7137.  
  7138.     SYNTAX
  7139.     #include <stdio.h>
  7140.     #include <stdlib.h>
  7141.     int r = system(buf);
  7142.     const char *buf;
  7143.  
  7144.     DESCRIPTION
  7145.     system calls the system shell with the specified command line,
  7146.     returning the exit code of the command or -1 if it was unable to run
  7147.     the command.
  7148.  
  7149.     || NOTE: FOR PROGRAMS COMPILED UNDER 1.3, even if run in 2.0
  7150.     || enviroment, the system call will not return the exit code from the
  7151.     || command, but return -1 if could not be run, 0 if it could.
  7152.  
  7153.     || NOTE: FOR PROGRAMS COMPILED UNDER 2.0, system will use the 2.0
  7154.     || calls and return a proper exit code when running under 2.0, and
  7155.     || will use execute if running under 1.3.
  7156.  
  7157.     INPUTS
  7158.     char *buf;        command line to run, like a normal AmigaDOS CLI
  7159.                 command line.
  7160.  
  7161.     RESULTS
  7162.     int r;            return code
  7163.  
  7164.     EXAMPLE
  7165.     main()
  7166.     {
  7167.     int r;
  7168.         r = system("c:type s:startup-sequence");
  7169.         printf("System returns %d\n", r);
  7170.     }
  7171.  
  7172. dice/tan,ftan                                                  dice/tan,ftan
  7173.  
  7174.     FUNCTION
  7175.     tan: return tan of a double quantity (ANSI)
  7176.     ftan: return tan of a float quantity (ANSI)
  7177.  
  7178.     LIBRARY
  7179.     m.lib
  7180.  
  7181.     SYNTAX
  7182.     #include <math.h>
  7183.     double a = tan(b); double b;
  7184.     float  c = ftan(d); float  d;
  7185.  
  7186.     DESCRIPTION
  7187.     tan returns the tangent of a double quantity; ftan returns the
  7188.     tangent of a floating point quantity.
  7189.  
  7190.     INPUTS
  7191.     double b;        double floating point value
  7192.  
  7193.     float d;        float floating point value
  7194.  
  7195.     RESULTS
  7196.     double a;        double floating point value
  7197.  
  7198.     float c;        float floating point value
  7199.  
  7200.     SEE ALSO
  7201.     acos, asin, atan, cos, exp, fabs, log, log10, pow, sin, sqrt, tan,
  7202.     facos, fasin
  7203.  
  7204.     EXAMPLE
  7205.     /*
  7206.      *  compile with the math library -lm
  7207.      */
  7208.     #include <math.h>
  7209.     #include <stdio.h>
  7210.     main()
  7211.     {
  7212.        {
  7213.           double a = tan(0.25);
  7214.           printf("tan 0.25 = %lf\n", a); /* 0.2553 */
  7215.        }
  7216.        {  /*  less accuracy   */
  7217.           float a = ftan(0.25);
  7218.           printf("tan 0.25 = %lf\n", (double)a);
  7219.        }
  7220.        return(0);
  7221.     }
  7222.  
  7223. dice/time                                                          dice/time
  7224.  
  7225.     FUNCTION
  7226.     get current time (ANSI)
  7227.  
  7228.     SYNTAX
  7229.     #include <time.h>
  7230.     time_t t = time(NULL);
  7231.     or
  7232.     time(&t);
  7233.     time_t t;
  7234.  
  7235.     DESCRIPTION
  7236.     time returns the current time as a time_t and also copies it into a
  7237.     time_t if the address of said is passed as an argument. You may pass
  7238.     NULL as an argument in which case the time is only returned.
  7239.  
  7240.     The time is returned as seconds since some base date, time_t is
  7241.     normally an unsigned long.
  7242.  
  7243.     INPUTS
  7244.     time_t *t;        pointer to a time_t or NULL
  7245.  
  7246.     RESULTS
  7247.     time_t t;        a time_t
  7248.  
  7249.     SEE ALSO
  7250.     time, localtime, asctime, strftime, ctime, clock
  7251.  
  7252.     EXAMPLE
  7253.     #include <stdio.h>
  7254.     #include <time.h>
  7255.  
  7256.     main()
  7257.     {
  7258.        time_t t = time(NULL);
  7259.        printf("t = %u\n", t);
  7260.        return(0);
  7261.     }
  7262.  
  7263. dice/tmpfile                                                    dice/tmpfile
  7264.  
  7265.     FUNCTION
  7266.     create a temporary file (ANSI)
  7267.  
  7268.     SYNTAX
  7269.     #include <stdio.h>
  7270.     FILE *fp = tmpfile(void);
  7271.  
  7272.     DESCRIPTION
  7273.     tmpfile creates a temporary file and returns a file pointer.  The
  7274.     name of the file is not accessible. The file pointer is available or
  7275.     reading, writing, and seeking (as in rewind, fseek).  The file is
  7276.     initially empty.  This call may be used to create a temporary file
  7277.     that will automatically be removed when you fclose it.    tmpfile
  7278.     returns a FILE pointer or NULL if it was unable to create the file.
  7279.  
  7280.     INPUTS
  7281.  
  7282.     none
  7283.  
  7284.     RESULTS
  7285.     FILE *fp;        opened temporary file
  7286.  
  7287.     SEE ALSO
  7288.     tmpnam, fopen, fclose
  7289.  
  7290.     EXAMPLE
  7291.     #include <stdio.h>
  7292.     #include <assert.h>
  7293.     main()
  7294.     {
  7295.        FILE *fp = tmpfile();
  7296.        char buf[256];
  7297.        assert(fp);
  7298.        fputs("This is a test of\n"
  7299.             "a temporary file\n"
  7300.             "fubar bletch\n", fp);
  7301.        rewind(fp);
  7302.        while (fgets(buf, sizeof(buf), fp))
  7303.        {
  7304.           fputs(buf, stdout);
  7305.        }
  7306.        fclose(fp);      /* close and delete the file     */
  7307.        return(0);
  7308.     }
  7309.  
  7310. dice/tmpnam                                                      dice/tmpnam
  7311.  
  7312.     FUNCTION
  7313.     create a unique, temporary file name (ANSI)
  7314.  
  7315.     SYNTAX
  7316.     #include <stdio.h>
  7317.     char *filename = tmpnam(buf);
  7318.     char *buf;
  7319.  
  7320.     DESCRIPTION
  7321.     tmpnam creates a unique temporary file name meant never to be seen by
  7322.     the user.  The filename tmpnam creates will be no more than L_tmpnam
  7323.     bytes long (L_tmpnam is a macro in <stdio.h>), including the NULL so
  7324.      you can simply declare a buffer: char buf[L_tmpnam];.
  7325.     tmpnam returns the buffer into which it created the temporary file
  7326.     name.  If you specify a non-NULL buffer it returns its first
  7327.     argument.  If you pass NULL to tmpnam then tmpnam will use its down
  7328.     internal static buffer (overwritting any previous name that was
  7329.     stored in said buffer) and return a pointer to that.
  7330.  
  7331.     INPUTS
  7332.     char *buf;        optional buffer of at least L_tmpnam bytes to
  7333.                 hold the temporary file name or NULL to have
  7334.                 tmpnam() use its own internal buffer.
  7335.  
  7336.     RESULTS
  7337.     char *ptr;        pointer to buffer (buf if buf != NULL)
  7338.  
  7339.     SEE ALSO
  7340.     tmpfile
  7341.  
  7342.     EXAMPLE
  7343.     #include <stdio.h>
  7344.     #include <assert.h>
  7345.     main()
  7346.     {
  7347.        char buf[L_tmpnam];      char *ptr;
  7348.        ptr = tmpnam(NULL);      puts(ptr);
  7349.        assert(tmpnam(buf) == buf);
  7350.        /*  returns argument */
  7351.        puts(buf);      /*  haven't overwritten it yet */
  7352.        printf("%s (same as first)\n", ptr);
  7353.        assert(tmpnam(NULL) == ptr);
  7354.        /*  that will overwrite it! */
  7355.        puts(ptr);
  7356.        return(0);
  7357.     }
  7358.  
  7359. dice/tolower,toupper                                    dice/tolower,toupper
  7360.  
  7361.     FUNCTION
  7362.     convert a character into lower or upper case (ANSI)
  7363.  
  7364.     SYNTAX
  7365.     #include <ctype.h>
  7366.     int lc = tolower(c);
  7367.     int uc = toupper(c);
  7368.     int c;
  7369.  
  7370.     || NOTE: These are MACROS if you #include <ctype.h>, subroutine calls
  7371.     || if you do not.
  7372.  
  7373.     DESCRIPTION
  7374.     toupper converts a character from lower to upper case, while tolower
  7375.     does the reverse.  Characters that are not of type upper or lower
  7376.     case (see isupper and islower) are left unchanged.
  7377.  
  7378.     || NOTE: Characters in the -1 to 255 range are valid inputs.
  7379.     || Characters less than -1 or larger than 255 are illegal and the
  7380.     || results will be random.  If you are passing a CHAR, you must cast
  7381.     || it to an UNSIGNED CHAR first. EOF is a valid input an always
  7382.     || returns false (zero).
  7383.  
  7384.     INPUTS
  7385.     int c;            character to be converted
  7386.  
  7387.     RESULTS
  7388.     int lc, uc;        converted character
  7389.  
  7390.     SEE ALSO
  7391.     isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint,
  7392.     ispunct, isspace, isupper, isxdigit
  7393.  
  7394.     EXAMPLE
  7395.     #include <stdio.h>
  7396.     #include <ctype.h>
  7397.     main()
  7398.     {
  7399.     int c;
  7400.  
  7401.         /* Print all 256 characters, showing conversions */
  7402.         for( c=0; c<256; c++) {
  7403.         if( (c & 31) == 0 )    /* 32 per line */
  7404.             printf("\n%02x: ", c);
  7405.         if( isprint( c ) )    /* if printable ... */
  7406.             printf("%c", tolower( c ) );
  7407.         else
  7408.             printf("%c", 127);  /* if not-printable */
  7409.         }
  7410.         printf("\n");
  7411.         return( 0 );
  7412.     }
  7413.  
  7414. dice/ungetc                                                      dice/ungetc
  7415.  
  7416.     FUNCTION
  7417.     push a character back onto a file pointer's input stream (ANSI)
  7418.  
  7419.     SYNTAX
  7420.     #include <stdio.h>
  7421.     int r = ungetc(c, fp);
  7422.     int c;
  7423.     FILE *fp;
  7424.  
  7425.     DESCRIPTION
  7426.     ungetc pushes the specified character back onto the input stream, as
  7427.     if it had not been read.  Only ONE character may be pushed back onto
  7428.     an input stream at a time.  If all went well, the return value r is
  7429.     equal to c.  Else EOF is returned if too many characters were pushed
  7430.     back. Some implementations of C allow multiple characters to be
  7431.      pushed back.  The majority, including DICE, allows only one.
  7432.     ungetc is useful when, in scanning an input stream, you overshoot the
  7433.     'last' character you wanted a particular routine to retrieve.  This
  7434.     routine can push the character back onto the input stream with ungetc
  7435.     so another routine's getc (getchar, fread, fgetc, etc...) will get
  7436.     that character back.
  7437.  
  7438.     INPUTS
  7439.     int c;            character to push back onto input stream
  7440.  
  7441.     FILE *fp;        file pointer stream to push character on to.
  7442.  
  7443.     RESULTS
  7444.     int r;            pushed character (c) if no error, EOF if error
  7445.  
  7446.     SEE ALSO
  7447.     getc, getchar, fread, fgetc
  7448.  
  7449.     EXAMPLE
  7450.     #include <stdio.h>
  7451.     #include <ctype.h>
  7452.     main()
  7453.     {
  7454.        void scan_number();
  7455.        void scan_alpha();
  7456.        puts("Enter nnnaaannn where n=digit a=alpha");
  7457.        puts("Example: 1234abcd99");
  7458.        printf("? ");
  7459.        fflush(stdout);
  7460.        scan_number();  puts("--");
  7461.        scan_alpha();   puts("--");
  7462.        scan_number(); return(0);
  7463.     }
  7464.     static void scan_number()
  7465.     {
  7466.        short c;
  7467.        for (c = getchar(); c >= '0' && c <= '9';
  7468.          c = getchar())
  7469.        {
  7470.            printf("digit: %c\n", c);
  7471.        }
  7472.        if (c != EOF)
  7473.           ungetc(c, stdin);
  7474.     }
  7475.     static void scan_alpha()
  7476.     {
  7477.        short c;
  7478.        for (c = getchar();
  7479.        tolower(c) >= 'a' && tolower(c) <= 'z';
  7480.        c =getchar())
  7481.        {
  7482.           printf("alpha: %c\n", c);
  7483.        }
  7484.           if (c != EOF) ungetc(c, stdin);
  7485.     }
  7486.  
  7487. dice/unlink                                                      dice/unlink
  7488.  
  7489.     FUNCTION
  7490.     delete a file (UNIX)
  7491.  
  7492.     SYNTAX
  7493.     #include <stdio.h>
  7494.     int r = unlink(filename);
  7495.     char *filename;
  7496.  
  7497.     DESCRIPTION
  7498.     unlink deletes a file, equivalent to remove. This call deletes a file
  7499.     from the filesystem.  unlink exists for UNIX compatibility only. Use
  7500.     rmdir to delete a directory if you wish to maintain portability.
  7501.  
  7502.     INPUTS
  7503.     char *filename;     name of file to delete
  7504.  
  7505.     RESULTS
  7506.     int r;            0 if successful, non-zero if error
  7507.  
  7508.     SEE ALSO
  7509.     close, creat, fcntl, fdtofh, ioctl, isatty, lseek, mkdir, open, read,
  7510.     rmdir, unlink, write
  7511.  
  7512.     EXAMPLE
  7513.     main()
  7514.     {
  7515.        int r;
  7516.        r = unlink("T:xx");
  7517.        if (r == 0)
  7518.           puts("Deleted T:xx");
  7519.        else
  7520.           puts("Unable to delete t:xx or it does not exist");
  7521.        return(0);
  7522.     }
  7523.  
  7524. dice/wbmain                                                      dice/wbmain
  7525.  
  7526.     FUNCTION
  7527.     main program entry when run from Workbench (DICE)
  7528.  
  7529.     SYNTAX
  7530.     #include <startup.h>
  7531.     int wbmain(struct WBStartup *wbs)
  7532.     {
  7533.         /*    your main code goes here */
  7534.         return(exitcode);
  7535.     }
  7536.  
  7537.     DESCRIPTION
  7538.     The wbmain routine is the entry point called after normal
  7539.     initialization of c.lib and the program enviroment is done by the
  7540.     startup module (c.o) and _main() routine (in c.lib). wbmain is called
  7541.     when the program is run from the workbench, main is called when the
  7542.     program is run from the CLI.  The exit code is ignored. The standard
  7543.     workbench startup message is passed to wbmain, you can process or
  7544.     ignore this message as you like but should NOT ReplyMsg it.  We
  7545.     repeat, do not ReplyMsg it.  When you return from wbmain or exit out
  7546.     of the program the exit code will automatically deal with the
  7547.     message.
  7548.  
  7549.     SEE ALSO
  7550.     main
  7551.  
  7552.     EXAMPLE
  7553.     /*
  7554.     *  If run from the workbench this program will create a
  7555.     *  file T:XX instead of printing something on the
  7556.     *  console (since there is no console in that case).
  7557.     */
  7558.     #include <stdio.h>
  7559.     int main(int ac, char**av)
  7560.     {
  7561.        puts("This was run from a CLI");
  7562.        return(0);
  7563.     }
  7564.     int wbmain(msg)
  7565.     void *msg;  /* cheat to make the example less complex */
  7566.     {
  7567.        FILE *fi = fopen("T:xx", "w");
  7568.        fprintf(fi, "This was run from the WORKBENCH\n");
  7569.        fclose(fi);
  7570.     }
  7571.  
  7572. dice/write                                                        dice/write
  7573.  
  7574.     FUNCTION
  7575.     write data to a file (UNIX)
  7576.  
  7577.     SYNTAX
  7578.     #include <fcntl.h>
  7579.     int r = write(fd, buf, bytes);
  7580.     int fd;
  7581.     void *buf;
  7582.     int bytes;
  7583.  
  7584.     DESCRIPTION
  7585.     write writes data to a file, starting at the current seek position.
  7586.     It extends the file if necessary, or else writes over existing data.
  7587.     With normal files, write will always return the number of bytes
  7588.     requested and fewer only if an error occurs.  With devices write may
  7589.     or may not return the number of bytes requested depending on the
  7590.     device, though usually it does.
  7591.  
  7592.     || NOTE: Refer to the file_descriptor manual page for general
  7593.     || information. Unlike file pointers and file handles, the file
  7594.     || descriptor is checked for validity and will simply return an error
  7595.     || if illegal.
  7596.  
  7597.     INPUTS
  7598.     int fd;         file descriptor to write to void
  7599.  
  7600.     *buf;            pointer to buffer to write data from
  7601.  
  7602.     int len;        number of bytes to write
  7603.  
  7604.     RESULTS
  7605.     int r;            number of bytes actually written, usually an
  7606.                 error if r !=len.
  7607.  
  7608.     SEE ALSO
  7609.     close, creat, fcntl, fdtofh, ioctl, isatty, lseek, mkdir, open, read,
  7610.     rmdir, unlink
  7611.  
  7612.     EXAMPLE
  7613.     See open for an example
  7614.  
  7615. dice/x.o                                                            dice/x.o
  7616.  
  7617.     FUNCTION
  7618.     autoinit terminating tags (DICE)
  7619.  
  7620.     LIBRARY
  7621.     x.o
  7622.  
  7623.     DESCRIPTION
  7624.     The X.O module is the normally the last object module specified when
  7625.     linking.  DICE has a very unique and elegant system of creating
  7626.     automatic initialization and exit code.  The X.O module terminates
  7627.      these special sections.
  7628.     Autoinit/exit sections work as follows: any object module may define
  7629.     a specially named section which will be linked, in sequence, with
  7630.     other module's sections of the same name.  These sections contain
  7631.     only code and no RTS.  The terminating module X.O adds a single RTS
  7632.     to each section, allowing the base of the section to be called by the
  7633.     startup/exit module (C.O).  Execution propigates through all
  7634.     autoinit/exit routines before hitting the RTS placed in the section
  7635.     by X.O. DICE uses autoinit/exit sections to handle the following
  7636.     things:
  7637.  
  7638.     1) Code to initialize initialized data containing references to other
  7639.        initialized data (i.e.  int a, *b = &a;) when the code must be
  7640.        made residentable.  This precludes the need for the startup code
  7641.        to handle Data-Data 32-bit relocations for resident code.
  7642.  
  7643.     2) Code to open libraries whos base variables are referenced but
  7644.        never declared. _DOSBase and the various floating point libraries
  7645.        are automatically opened in this way whenever library calls to
  7646.        them are made.  This precludes the need for DICE to have massive,
  7647.        complex, and many times unncessary code in c.lib to handle these
  7648.        situations.
  7649.  
  7650.     3) Code to close libraries that were openned by (2) on exit.
  7651.  
  7652.     4) Entry points for the special __autoinit keyword.
  7653.  
  7654.     SEE ALSO
  7655.     c.o
  7656.  
  7657.     EXAMPLE
  7658.     dlink dlib:c.o myprog.o dlib:x.o -o myprog -v
  7659.